Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 Stephen Warren |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <asm/cache.h> |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 10 | #include <asm/io.h> |
Matthias Brugger | 8e3361c | 2019-11-19 16:01:03 +0100 | [diff] [blame] | 11 | #include <asm/arch/base.h> |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 12 | #include <asm/arch/mbox.h> |
Stephen Warren | 122426d | 2015-04-06 20:28:39 -0600 | [diff] [blame] | 13 | #include <phys2bus.h> |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 14 | |
Stephen Warren | 004c105 | 2014-01-13 19:50:12 -0700 | [diff] [blame] | 15 | #define TIMEOUT 1000 /* ms */ |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 16 | |
| 17 | int bcm2835_mbox_call_raw(u32 chan, u32 send, u32 *recv) |
| 18 | { |
| 19 | struct bcm2835_mbox_regs *regs = |
| 20 | (struct bcm2835_mbox_regs *)BCM2835_MBOX_PHYSADDR; |
| 21 | ulong endtime = get_timer(0) + TIMEOUT; |
| 22 | u32 val; |
| 23 | |
| 24 | debug("time: %lu timeout: %lu\n", get_timer(0), endtime); |
| 25 | |
| 26 | if (send & BCM2835_CHAN_MASK) { |
| 27 | printf("mbox: Illegal mbox data 0x%08x\n", send); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | /* Drain any stale responses */ |
| 32 | |
| 33 | for (;;) { |
Fabian Vogt | 4982244 | 2019-07-16 13:09:47 +0200 | [diff] [blame] | 34 | val = readl(®s->mail0_status); |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 35 | if (val & BCM2835_MBOX_STATUS_RD_EMPTY) |
| 36 | break; |
| 37 | if (get_timer(0) >= endtime) { |
| 38 | printf("mbox: Timeout draining stale responses\n"); |
| 39 | return -1; |
| 40 | } |
| 41 | val = readl(®s->read); |
| 42 | } |
| 43 | |
| 44 | /* Wait for space to send */ |
| 45 | |
| 46 | for (;;) { |
Fabian Vogt | 4982244 | 2019-07-16 13:09:47 +0200 | [diff] [blame] | 47 | val = readl(®s->mail1_status); |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 48 | if (!(val & BCM2835_MBOX_STATUS_WR_FULL)) |
| 49 | break; |
| 50 | if (get_timer(0) >= endtime) { |
| 51 | printf("mbox: Timeout waiting for send space\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* Send the request */ |
| 57 | |
| 58 | val = BCM2835_MBOX_PACK(chan, send); |
| 59 | debug("mbox: TX raw: 0x%08x\n", val); |
| 60 | writel(val, ®s->write); |
| 61 | |
| 62 | /* Wait for the response */ |
| 63 | |
| 64 | for (;;) { |
Fabian Vogt | 4982244 | 2019-07-16 13:09:47 +0200 | [diff] [blame] | 65 | val = readl(®s->mail0_status); |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 66 | if (!(val & BCM2835_MBOX_STATUS_RD_EMPTY)) |
| 67 | break; |
| 68 | if (get_timer(0) >= endtime) { |
| 69 | printf("mbox: Timeout waiting for response\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Read the response */ |
| 75 | |
| 76 | val = readl(®s->read); |
| 77 | debug("mbox: RX raw: 0x%08x\n", val); |
| 78 | |
| 79 | /* Validate the response */ |
| 80 | |
| 81 | if (BCM2835_MBOX_UNPACK_CHAN(val) != chan) { |
| 82 | printf("mbox: Response channel mismatch\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | *recv = BCM2835_MBOX_UNPACK_DATA(val); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | #ifdef DEBUG |
| 92 | void dump_buf(struct bcm2835_mbox_hdr *buffer) |
| 93 | { |
| 94 | u32 *p; |
| 95 | u32 words; |
| 96 | int i; |
| 97 | |
| 98 | p = (u32 *)buffer; |
| 99 | words = buffer->buf_size / 4; |
| 100 | for (i = 0; i < words; i++) |
| 101 | printf(" 0x%04x: 0x%08x\n", i * 4, p[i]); |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | int bcm2835_mbox_call_prop(u32 chan, struct bcm2835_mbox_hdr *buffer) |
| 106 | { |
| 107 | int ret; |
| 108 | u32 rbuffer; |
| 109 | struct bcm2835_mbox_tag_hdr *tag; |
| 110 | int tag_index; |
| 111 | |
| 112 | #ifdef DEBUG |
| 113 | printf("mbox: TX buffer\n"); |
| 114 | dump_buf(buffer); |
| 115 | #endif |
| 116 | |
Alexander Stein | 4342557 | 2015-07-24 09:22:13 +0200 | [diff] [blame] | 117 | flush_dcache_range((unsigned long)buffer, |
| 118 | (unsigned long)((void *)buffer + |
| 119 | roundup(buffer->buf_size, ARCH_DMA_MINALIGN))); |
| 120 | |
Stephen Warren | 2b51315 | 2016-03-16 21:40:57 -0600 | [diff] [blame] | 121 | ret = bcm2835_mbox_call_raw(chan, |
| 122 | phys_to_bus((unsigned long)buffer), |
| 123 | &rbuffer); |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 124 | if (ret) |
| 125 | return ret; |
Alexander Stein | 4342557 | 2015-07-24 09:22:13 +0200 | [diff] [blame] | 126 | |
| 127 | invalidate_dcache_range((unsigned long)buffer, |
| 128 | (unsigned long)((void *)buffer + |
| 129 | roundup(buffer->buf_size, ARCH_DMA_MINALIGN))); |
| 130 | |
Stephen Warren | 2b51315 | 2016-03-16 21:40:57 -0600 | [diff] [blame] | 131 | if (rbuffer != phys_to_bus((unsigned long)buffer)) { |
Stephen Warren | 8807728 | 2013-01-29 16:37:36 +0000 | [diff] [blame] | 132 | printf("mbox: Response buffer mismatch\n"); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | #ifdef DEBUG |
| 137 | printf("mbox: RX buffer\n"); |
| 138 | dump_buf(buffer); |
| 139 | #endif |
| 140 | |
| 141 | /* Validate overall response status */ |
| 142 | |
| 143 | if (buffer->code != BCM2835_MBOX_RESP_CODE_SUCCESS) { |
| 144 | printf("mbox: Header response code invalid\n"); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | /* Validate each tag's response status */ |
| 149 | |
| 150 | tag = (void *)(buffer + 1); |
| 151 | tag_index = 0; |
| 152 | while (tag->tag) { |
| 153 | if (!(tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE)) { |
| 154 | printf("mbox: Tag %d missing val_len response bit\n", |
| 155 | tag_index); |
| 156 | return -1; |
| 157 | } |
| 158 | /* |
| 159 | * Clear the reponse bit so clients can just look right at the |
| 160 | * length field without extra processing |
| 161 | */ |
| 162 | tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE; |
| 163 | tag = (void *)(((u8 *)tag) + sizeof(*tag) + tag->val_buf_size); |
| 164 | tag_index++; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |