Skip to content
Permalink
Browse files

Add support for B+, Pi2, and 4.1 kernels

  • Loading branch information...
Richard Hirst
Richard Hirst committed Oct 3, 2015
1 parent bf455ee commit 96014c804d68677baf4fff25178f9b152c1f31e6
@@ -44,15 +44,15 @@
#define DMA_CONBLK_AD (0x04/4)
#define DMA_DEBUG (0x20/4)

#define GPIO_BASE 0x20200000
#define GPIO_BASE 0x3f200000
#define GPIO_LEN 0xB4
#define DMA_BASE 0x20007000
#define DMA_BASE 0x3f007000
#define DMA_LEN DMA_CHAN_SIZE * (DMA_CHAN_MAX+1)
#define PWM_BASE 0x2020C000
#define PWM_BASE 0x3f20C000
#define PWM_LEN 0x28
#define CLK_BASE 0x20101000
#define CLK_BASE 0x3f101000
#define CLK_LEN 0xA8
#define TICK_BASE 0x20003000
#define TICK_BASE 0x3f003000
#define TICK_LEN 0x08

#define PWM_CTL (0x00/4)
@@ -2,8 +2,8 @@
.PHONY: all install uninstall
all: servod

servod: servod.c
gcc -Wall -g -O2 -o servod servod.c -lm
servod: servod.c mailbox.c
gcc -Wall -g -O2 -o servod servod.c mailbox.c -lm

install: servod
[ "`id -u`" = "0" ] || { echo "Must be run as root"; exit 1; }
@@ -15,7 +15,12 @@ PATH=/sbin:/usr/sbin:/bin:/usr/bin

OPTS="--idle-timeout=2000"

res=0
STATUSFILE="/tmp/servoblaster-status"

if [ $( id -u ) != 0 ]; then
echo "ERROR: Must be run as root"
exit 1
fi

case "$1" in
start)
@@ -29,13 +34,31 @@ case "$1" in
killall servod
;;
status)
[ -e /dev/servoblaster ] || res=4
if [ ! -e /dev/servoblaster ]; then
echo "ERROR: /dev/servoblaster does not exist"
exit 2
fi
rm -f $STATUSFILE
echo "status $STATUSFILE" > /dev/servoblaster
sleep 0.2
if [ ! -e $STATUSFILE ]; then
echo "ERROR: servod not responding"
exit 3
elif grep -q "^OK" $STATUSFILE; then
echo "OK"
exit 0
elif grep "^ERROR:" $STATUSFILE; then
exit 4
else
echo "ERROR: No status from servod"
exit 5
fi
;;
*)
echo "Usage: servoblaster [start|stop|status]" >&2
res=3
exit 6
;;
esac

exit $res
exit 0

@@ -0,0 +1,298 @@
/*
Copyright (c) 2012, Broadcom Europe Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "mailbox.h"

//#define DEBUG

#define PAGE_SIZE (4*1024)

void *mapmem(unsigned base, unsigned size)
{
int mem_fd;
unsigned offset = base % PAGE_SIZE;
base = base - offset;
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem\nThis program should be run as root. Try prefixing command with: sudo\n");
exit (-1);
}
void *mem = mmap(
0,
size,
PROT_READ|PROT_WRITE,
MAP_SHARED/*|MAP_FIXED*/,
mem_fd,
base);
#ifdef DEBUG
printf("base=0x%x, mem=%p\n", base, mem);
#endif
if (mem == MAP_FAILED) {
printf("mmap error %d\n", (int)mem);
exit (-1);
}
close(mem_fd);
return (char *)mem + offset;
}

void *unmapmem(void *addr, unsigned size)
{
int s = munmap(addr, size);
if (s != 0) {
printf("munmap error %d\n", s);
exit (-1);
}

return NULL;
}

/*
* use ioctl to send mbox property message
*/

static int mbox_property(int file_desc, void *buf)
{
int fd = file_desc;
int ret_val = -1;

if (fd < 0) {
fd = mbox_open();
}
if (fd >= 0) {
ret_val = ioctl(fd, IOCTL_MBOX_PROPERTY, buf);

if (ret_val < 0) {
printf("ioctl_set_msg failed, errno %d: %m\n", errno);
}
}
#ifdef DEBUG
unsigned *p = buf; int i; unsigned size = *(unsigned *)buf;
for (i=0; i<size/4; i++)
printf("%04x: 0x%08x\n", i*sizeof *p, p[i]);
#endif
if (file_desc < 0)
mbox_close(fd);

return ret_val;
}

unsigned mem_alloc(int file_desc, unsigned size, unsigned align, unsigned flags)
{
int i=0;
unsigned p[32];

#ifdef DEBUG
printf("Requesting %d bytes\n", size);
#endif
p[i++] = 0; // size
p[i++] = 0x00000000; // process request

p[i++] = 0x3000c; // (the tag id)
p[i++] = 12; // (size of the buffer)
p[i++] = 12; // (size of the data)
p[i++] = size; // (num bytes? or pages?)
p[i++] = align; // (alignment)
p[i++] = flags; // (MEM_FLAG_L1_NONALLOCATING)

p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size

if (mbox_property(file_desc, p) < 0)
return -1;
else
return p[5];
}

unsigned mem_free(int file_desc, unsigned handle)
{
int i=0;
unsigned p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request

p[i++] = 0x3000f; // (the tag id)
p[i++] = 4; // (size of the buffer)
p[i++] = 4; // (size of the data)
p[i++] = handle;

p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size

mbox_property(file_desc, p);
return p[5];
}

unsigned mem_lock(int file_desc, unsigned handle)
{
int i=0;
unsigned p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request

p[i++] = 0x3000d; // (the tag id)
p[i++] = 4; // (size of the buffer)
p[i++] = 4; // (size of the data)
p[i++] = handle;

p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size

if (mbox_property(file_desc, p) < 0)
return ~0;
else
return p[5];
}

unsigned mem_unlock(int file_desc, unsigned handle)
{
int i=0;
unsigned p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request

p[i++] = 0x3000e; // (the tag id)
p[i++] = 4; // (size of the buffer)
p[i++] = 4; // (size of the data)
p[i++] = handle;

p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size

mbox_property(file_desc, p);
return p[5];
}

unsigned execute_code(int file_desc, unsigned code, unsigned r0, unsigned r1, unsigned r2, unsigned r3, unsigned r4, unsigned r5)
{
int i=0;
unsigned p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request

p[i++] = 0x30010; // (the tag id)
p[i++] = 28; // (size of the buffer)
p[i++] = 28; // (size of the data)
p[i++] = code;
p[i++] = r0;
p[i++] = r1;
p[i++] = r2;
p[i++] = r3;
p[i++] = r4;
p[i++] = r5;

p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size

mbox_property(file_desc, p);
return p[5];
}

unsigned qpu_enable(int file_desc, unsigned enable)
{
int i=0;
unsigned p[32];

p[i++] = 0; // size
p[i++] = 0x00000000; // process request

p[i++] = 0x30012; // (the tag id)
p[i++] = 4; // (size of the buffer)
p[i++] = 4; // (size of the data)
p[i++] = enable;

p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size

mbox_property(file_desc, p);
return p[5];
}

unsigned execute_qpu(int file_desc, unsigned num_qpus, unsigned control, unsigned noflush, unsigned timeout) {
int i=0;
unsigned p[32];

p[i++] = 0; // size
p[i++] = 0x00000000; // process request
p[i++] = 0x30011; // (the tag id)
p[i++] = 16; // (size of the buffer)
p[i++] = 16; // (size of the data)
p[i++] = num_qpus;
p[i++] = control;
p[i++] = noflush;
p[i++] = timeout; // ms

p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size

mbox_property(file_desc, p);
return p[5];
}

int mbox_open(void) {
int file_desc;
char filename[64];

// open a char device file used for communicating with kernel mbox driver
if ((file_desc = open("/dev/vcio", 0)) >= 0) {
/* New kernel, we use /dev/vcio, major 249, since kernel 4.1 */
return file_desc;
}

/* Most likely an old kernel, so drop back to the old major=100 device */
sprintf(filename, "/tmp/mailbox-%d", getpid());
unlink(filename);
if (mknod(filename, S_IFCHR|0600, makedev(100, 0)) < 0) {
printf("Failed to create mailbox device %s: %m\n", filename);
return -1;
}
file_desc = open(filename, 0);
if (file_desc < 0) {
printf("Can't open device file %s: %m\n", filename);
unlink(filename);
return -1;
}
unlink(filename);

return file_desc;
}

void mbox_close(int file_desc) {
close(file_desc);
}

0 comments on commit 96014c8

Please sign in to comment.
You can’t perform that action at this time.