1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/sx.h>
40 #include <sys/rman.h>
41 #include <machine/bus.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <arm/broadcom/bcm2835/bcm2835_mbox.h>
47 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
48 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
49 
50 #include "mbox_if.h"
51 
52 #define	REG_READ	0x00
53 #define	REG_POL		0x10
54 #define	REG_SENDER	0x14
55 #define	REG_STATUS	0x18
56 #define		STATUS_FULL	0x80000000
57 #define		STATUS_EMPTY	0x40000000
58 #define	REG_CONFIG	0x1C
59 #define		CONFIG_DATA_IRQ	0x00000001
60 #define	REG_WRITE	0x20 /* This is Mailbox 1 address */
61 
62 #define	MBOX_MSG(chan, data)	(((data) & ~0xf) | ((chan) & 0xf))
63 #define	MBOX_CHAN(msg)		((msg) & 0xf)
64 #define	MBOX_DATA(msg)		((msg) & ~0xf)
65 
66 #define	MBOX_LOCK(sc)	do {	\
67 	mtx_lock(&(sc)->lock);	\
68 } while(0)
69 
70 #define	MBOX_UNLOCK(sc)	do {		\
71 	mtx_unlock(&(sc)->lock);	\
72 } while(0)
73 
74 #ifdef  DEBUG
75 #define dprintf(fmt, args...) printf(fmt, ##args)
76 #else
77 #define dprintf(fmt, args...)
78 #endif
79 
80 struct bcm_mbox_softc {
81 	struct mtx		lock;
82 	struct resource *	mem_res;
83 	struct resource *	irq_res;
84 	void*			intr_hl;
85 	bus_space_tag_t		bst;
86 	bus_space_handle_t	bsh;
87 	int			msg[BCM2835_MBOX_CHANS];
88 	int			have_message[BCM2835_MBOX_CHANS];
89 	struct sx		property_chan_lock;
90 };
91 
92 #define	mbox_read_4(sc, reg)		\
93     bus_space_read_4((sc)->bst, (sc)->bsh, reg)
94 #define	mbox_write_4(sc, reg, val)		\
95     bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
96 
97 static struct ofw_compat_data compat_data[] = {
98 	{"broadcom,bcm2835-mbox",	1},
99 	{"brcm,bcm2835-mbox",		1},
100 	{NULL,				0}
101 };
102 
103 static int
104 bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan)
105 {
106 #ifdef DEBUG
107 	uint32_t data;
108 #endif
109 	uint32_t msg;
110 	int chan;
111 
112 	msg = mbox_read_4(sc, REG_READ);
113 	dprintf("bcm_mbox_intr: raw data %08x\n", msg);
114 	chan = MBOX_CHAN(msg);
115 #ifdef DEBUG
116 	data = MBOX_DATA(msg);
117 #endif
118 	if (sc->msg[chan]) {
119 		printf("bcm_mbox_intr: channel %d oveflow\n", chan);
120 		return (1);
121 	}
122 	dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
123 	sc->msg[chan] = msg;
124 
125 	if (ochan != NULL)
126 		*ochan = chan;
127 
128 	return (0);
129 }
130 
131 static void
132 bcm_mbox_intr(void *arg)
133 {
134 	struct bcm_mbox_softc *sc = arg;
135 	int chan;
136 
137 	MBOX_LOCK(sc);
138 	while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
139 		if (bcm_mbox_read_msg(sc, &chan) == 0) {
140 			sc->have_message[chan] = 1;
141 			wakeup(&sc->have_message[chan]);
142 		}
143 	MBOX_UNLOCK(sc);
144 }
145 
146 static int
147 bcm_mbox_probe(device_t dev)
148 {
149 
150 	if (!ofw_bus_status_okay(dev))
151 		return (ENXIO);
152 
153 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
154 		return (ENXIO);
155 
156 	device_set_desc(dev, "BCM2835 VideoCore Mailbox");
157 
158 	return (BUS_PROBE_DEFAULT);
159 }
160 
161 static int
162 bcm_mbox_attach(device_t dev)
163 {
164 	struct bcm_mbox_softc *sc = device_get_softc(dev);
165 	int i;
166 	int rid = 0;
167 
168 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
169 	if (sc->mem_res == NULL) {
170 		device_printf(dev, "could not allocate memory resource\n");
171 		return (ENXIO);
172 	}
173 
174 	sc->bst = rman_get_bustag(sc->mem_res);
175 	sc->bsh = rman_get_bushandle(sc->mem_res);
176 
177 	rid = 0;
178 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
179 	if (sc->irq_res == NULL) {
180 		device_printf(dev, "could not allocate interrupt resource\n");
181 		return (ENXIO);
182 	}
183 
184 	/* Setup and enable the timer */
185 	if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
186 	    NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
187 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
188 		device_printf(dev, "Unable to setup the clock irq handler.\n");
189 		return (ENXIO);
190 	}
191 
192 	mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
193 	for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
194 		sc->msg[i] = 0;
195 		sc->have_message[i] = 0;
196 	}
197 
198 	sx_init(&sc->property_chan_lock, "mboxprop");
199 
200 	/* Read all pending messages */
201 	while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0)
202 		(void)mbox_read_4(sc, REG_READ);
203 
204 	mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
205 
206 	return (0);
207 }
208 
209 /*
210  * Mailbox API
211  */
212 static int
213 bcm_mbox_write(device_t dev, int chan, uint32_t data)
214 {
215 	int limit = 1000;
216 	struct bcm_mbox_softc *sc = device_get_softc(dev);
217 
218 	dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
219 	MBOX_LOCK(sc);
220 	sc->have_message[chan] = 0;
221 	while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit)
222 		DELAY(5);
223 	if (limit == 0) {
224 		printf("bcm_mbox_write: STATUS_FULL stuck");
225 		MBOX_UNLOCK(sc);
226 		return (EAGAIN);
227 	}
228 	mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
229 	MBOX_UNLOCK(sc);
230 
231 	return (0);
232 }
233 
234 static int
235 bcm_mbox_read(device_t dev, int chan, uint32_t *data)
236 {
237 	struct bcm_mbox_softc *sc = device_get_softc(dev);
238 	int err, read_chan;
239 
240 	dprintf("bcm_mbox_read: chan %d\n", chan);
241 
242 	err = 0;
243 	MBOX_LOCK(sc);
244 	if (!cold) {
245 		if (sc->have_message[chan] == 0) {
246 			if (mtx_sleep(&sc->have_message[chan], &sc->lock, 0,
247 			    "mbox", 10*hz) != 0) {
248 				device_printf(dev, "timeout waiting for message on chan %d\n", chan);
249 				err = ETIMEDOUT;
250 			}
251 		}
252 	} else {
253 		do {
254 			/* Wait for a message */
255 			while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
256 				;
257 			/* Read the message */
258 			if (bcm_mbox_read_msg(sc, &read_chan) != 0) {
259 				err = EINVAL;
260 				goto out;
261 			}
262 		} while (read_chan != chan);
263 	}
264 	/*
265 	 *  get data from intr handler, the same channel is never coming
266 	 *  because of holding sc lock.
267 	 */
268 	*data = MBOX_DATA(sc->msg[chan]);
269 	sc->msg[chan] = 0;
270 	sc->have_message[chan] = 0;
271 out:
272 	MBOX_UNLOCK(sc);
273 	dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
274 
275 	return (err);
276 }
277 
278 static device_method_t bcm_mbox_methods[] = {
279 	DEVMETHOD(device_probe,		bcm_mbox_probe),
280 	DEVMETHOD(device_attach,	bcm_mbox_attach),
281 
282 	DEVMETHOD(mbox_read,		bcm_mbox_read),
283 	DEVMETHOD(mbox_write,		bcm_mbox_write),
284 
285 	DEVMETHOD_END
286 };
287 
288 static driver_t bcm_mbox_driver = {
289 	"mbox",
290 	bcm_mbox_methods,
291 	sizeof(struct bcm_mbox_softc),
292 };
293 
294 static devclass_t bcm_mbox_devclass;
295 
296 DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
297 
298 static void
299 bcm2835_mbox_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
300 {
301 	bus_addr_t *addr;
302 
303 	if (err)
304 		return;
305 	addr = (bus_addr_t *)arg;
306 	*addr = ARMC_TO_VCBUS(segs[0].ds_addr);
307 }
308 
309 static void *
310 bcm2835_mbox_init_dma(device_t dev, size_t len, bus_dma_tag_t *tag,
311     bus_dmamap_t *map, bus_addr_t *phys)
312 {
313 	void *buf;
314 	int err;
315 
316 	err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0,
317 	    bcm283x_dmabus_peripheral_lowaddr(), BUS_SPACE_MAXADDR, NULL, NULL,
318 	    len, 1, len, 0, NULL, NULL, tag);
319 	if (err != 0) {
320 		device_printf(dev, "can't create DMA tag\n");
321 		return (NULL);
322 	}
323 
324 	err = bus_dmamem_alloc(*tag, &buf, 0, map);
325 	if (err != 0) {
326 		bus_dma_tag_destroy(*tag);
327 		device_printf(dev, "can't allocate dmamem\n");
328 		return (NULL);
329 	}
330 
331 	err = bus_dmamap_load(*tag, *map, buf, len, bcm2835_mbox_dma_cb,
332 	    phys, 0);
333 	if (err != 0) {
334 		bus_dmamem_free(*tag, buf, *map);
335 		bus_dma_tag_destroy(*tag);
336 		device_printf(dev, "can't load DMA map\n");
337 		return (NULL);
338 	}
339 
340 	return (buf);
341 }
342 
343 static int
344 bcm2835_mbox_err(device_t dev, bus_addr_t msg_phys, uint32_t resp_phys,
345 	struct bcm2835_mbox_hdr *msg, size_t len)
346 {
347 	int idx;
348 	struct bcm2835_mbox_tag_hdr *tag;
349 	uint8_t *last;
350 
351 	if ((uint32_t)msg_phys != resp_phys) {
352 		device_printf(dev, "response channel mismatch\n");
353 		return (EIO);
354 	}
355 	if (msg->code != BCM2835_MBOX_CODE_RESP_SUCCESS) {
356 		device_printf(dev, "mbox response error\n");
357 		return (EIO);
358 	}
359 
360 	/* Loop until the end tag. */
361 	tag = (struct bcm2835_mbox_tag_hdr *)(msg + 1);
362 	last = (uint8_t *)msg + len;
363 	for (idx = 0; tag->tag != 0; idx++) {
364 		if ((tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE) == 0) {
365 			device_printf(dev, "tag %d response error\n", idx);
366 			return (EIO);
367 		}
368 		/* Clear the response bit. */
369 		tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;
370 
371 		/* Next tag. */
372 		tag = (struct bcm2835_mbox_tag_hdr *)((uint8_t *)tag +
373 		    sizeof(*tag) + tag->val_buf_size);
374 
375 		if ((uint8_t *)tag > last) {
376 			device_printf(dev, "mbox buffer size error\n");
377 			return (EIO);
378 		}
379 	}
380 
381 	return (0);
382 }
383 
384 int
385 bcm2835_mbox_property(void *msg, size_t msg_size)
386 {
387 	struct bcm_mbox_softc *sc;
388 	struct msg_set_power_state *buf;
389 	bus_dma_tag_t msg_tag;
390 	bus_dmamap_t msg_map;
391 	bus_addr_t msg_phys;
392 	uint32_t reg;
393 	device_t mbox;
394 	int err;
395 
396 	/* get mbox device */
397 	mbox = devclass_get_device(devclass_find("mbox"), 0);
398 	if (mbox == NULL)
399 		return (ENXIO);
400 
401 	sc = device_get_softc(mbox);
402 	sx_xlock(&sc->property_chan_lock);
403 
404 	/* Allocate memory for the message */
405 	buf = bcm2835_mbox_init_dma(mbox, msg_size, &msg_tag, &msg_map,
406 	    &msg_phys);
407 	if (buf == NULL) {
408 		err = ENOMEM;
409 		goto out;
410 	}
411 
412 	memcpy(buf, msg, msg_size);
413 
414 	bus_dmamap_sync(msg_tag, msg_map,
415 	    BUS_DMASYNC_PREWRITE);
416 
417 	MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys);
418 	MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, &reg);
419 
420 	bus_dmamap_sync(msg_tag, msg_map,
421 	    BUS_DMASYNC_PREREAD);
422 
423 	memcpy(msg, buf, msg_size);
424 
425 	err = bcm2835_mbox_err(mbox, msg_phys, reg,
426 	    (struct bcm2835_mbox_hdr *)msg, msg_size);
427 
428 	bus_dmamap_unload(msg_tag, msg_map);
429 	bus_dmamem_free(msg_tag, buf, msg_map);
430 	bus_dma_tag_destroy(msg_tag);
431 out:
432 	sx_xunlock(&sc->property_chan_lock);
433 	return (err);
434 }
435 
436 int
437 bcm2835_mbox_set_power_state(uint32_t device_id, boolean_t on)
438 {
439 	struct msg_set_power_state msg;
440 	int err;
441 
442 	memset(&msg, 0, sizeof(msg));
443 	msg.hdr.buf_size = sizeof(msg);
444 	msg.hdr.code = BCM2835_MBOX_CODE_REQ;
445 	msg.tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE;
446 	msg.tag_hdr.val_buf_size = sizeof(msg.body);
447 	msg.tag_hdr.val_len = sizeof(msg.body.req);
448 	msg.body.req.device_id = device_id;
449 	msg.body.req.state = (on ? BCM2835_MBOX_POWER_ON : 0) |
450 	    BCM2835_MBOX_POWER_WAIT;
451 	msg.end_tag = 0;
452 
453 	err = bcm2835_mbox_property(&msg, sizeof(msg));
454 
455 	return (err);
456 }
457 
458 int
459 bcm2835_mbox_get_clock_rate(uint32_t clock_id, uint32_t *hz)
460 {
461 	struct msg_get_clock_rate msg;
462 	int err;
463 
464 	memset(&msg, 0, sizeof(msg));
465 	msg.hdr.buf_size = sizeof(msg);
466 	msg.hdr.code = BCM2835_MBOX_CODE_REQ;
467 	msg.tag_hdr.tag = BCM2835_MBOX_TAG_GET_CLOCK_RATE;
468 	msg.tag_hdr.val_buf_size = sizeof(msg.body);
469 	msg.tag_hdr.val_len = sizeof(msg.body.req);
470 	msg.body.req.clock_id = clock_id;
471 	msg.end_tag = 0;
472 
473 	err = bcm2835_mbox_property(&msg, sizeof(msg));
474 	*hz = msg.body.resp.rate_hz;
475 
476 	return (err);
477 }
478 
479 int
480 bcm2835_mbox_fb_get_w_h(struct bcm2835_fb_config *fb)
481 {
482 	int err;
483 	struct msg_fb_get_w_h msg;
484 
485 	memset(&msg, 0, sizeof(msg));
486 	msg.hdr.buf_size = sizeof(msg);
487 	msg.hdr.code = BCM2835_MBOX_CODE_REQ;
488 	BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, GET_PHYSICAL_W_H);
489 	msg.physical_w_h.tag_hdr.val_len = 0;
490 	msg.end_tag = 0;
491 
492 	err = bcm2835_mbox_property(&msg, sizeof(msg));
493 	if (err == 0) {
494 		fb->xres = msg.physical_w_h.body.resp.width;
495 		fb->yres = msg.physical_w_h.body.resp.height;
496 	}
497 
498 	return (err);
499 }
500 
501 int
502 bcm2835_mbox_fb_get_bpp(struct bcm2835_fb_config *fb)
503 {
504 	int err;
505 	struct msg_fb_get_bpp msg;
506 
507 	memset(&msg, 0, sizeof(msg));
508 	msg.hdr.buf_size = sizeof(msg);
509 	msg.hdr.code = BCM2835_MBOX_CODE_REQ;
510 	BCM2835_MBOX_INIT_TAG(&msg.bpp, GET_DEPTH);
511 	msg.bpp.tag_hdr.val_len = 0;
512 	msg.end_tag = 0;
513 
514 	err = bcm2835_mbox_property(&msg, sizeof(msg));
515 	if (err == 0)
516 		fb->bpp = msg.bpp.body.resp.bpp;
517 
518 	return (err);
519 }
520 
521 int
522 bcm2835_mbox_fb_init(struct bcm2835_fb_config *fb)
523 {
524 	int err;
525 	struct msg_fb_setup msg;
526 
527 	memset(&msg, 0, sizeof(msg));
528 	msg.hdr.buf_size = sizeof(msg);
529 	msg.hdr.code = BCM2835_MBOX_CODE_REQ;
530 	BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, SET_PHYSICAL_W_H);
531 	msg.physical_w_h.body.req.width = fb->xres;
532 	msg.physical_w_h.body.req.height = fb->yres;
533 	BCM2835_MBOX_INIT_TAG(&msg.virtual_w_h, SET_VIRTUAL_W_H);
534 	msg.virtual_w_h.body.req.width = fb->vxres;
535 	msg.virtual_w_h.body.req.height = fb->vyres;
536 	BCM2835_MBOX_INIT_TAG(&msg.offset, SET_VIRTUAL_OFFSET);
537 	msg.offset.body.req.x = fb->xoffset;
538 	msg.offset.body.req.y = fb->yoffset;
539 	BCM2835_MBOX_INIT_TAG(&msg.depth, SET_DEPTH);
540 	msg.depth.body.req.bpp = fb->bpp;
541 	BCM2835_MBOX_INIT_TAG(&msg.alpha, SET_ALPHA_MODE);
542 	msg.alpha.body.req.alpha = BCM2835_MBOX_ALPHA_MODE_IGNORED;
543 	BCM2835_MBOX_INIT_TAG(&msg.buffer, ALLOCATE_BUFFER);
544 	msg.buffer.body.req.alignment = PAGE_SIZE;
545 	BCM2835_MBOX_INIT_TAG(&msg.pitch, GET_PITCH);
546 	msg.end_tag = 0;
547 
548 	err = bcm2835_mbox_property(&msg, sizeof(msg));
549 	if (err == 0) {
550 		fb->xres = msg.physical_w_h.body.resp.width;
551 		fb->yres = msg.physical_w_h.body.resp.height;
552 		fb->vxres = msg.virtual_w_h.body.resp.width;
553 		fb->vyres = msg.virtual_w_h.body.resp.height;
554 		fb->xoffset = msg.offset.body.resp.x;
555 		fb->yoffset = msg.offset.body.resp.y;
556 		fb->pitch = msg.pitch.body.resp.pitch;
557 		fb->base = VCBUS_TO_ARMC(msg.buffer.body.resp.fb_address);
558 		fb->size = msg.buffer.body.resp.fb_size;
559 	}
560 
561 	return (err);
562 }
563