From 2c52dac4ca5f5f0731e231f4842416ed43438997 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Mon, 17 Dec 2012 00:44:48 -0300 Subject: [PATCH] libubb/: helper library for UBB access --- libubb/Makefile | 34 +++++++++++++ libubb/include/ubb/regbase.h | 49 ++++++++++++++++++ libubb/include/ubb/regs4740.h | 75 ++++++++++++++++++++++++++++ libubb/include/ubb/ubb.h | 44 +++++++++++++++++ libubb/ubb.c | 93 +++++++++++++++++++++++++++++++++++ 5 files changed, 295 insertions(+) create mode 100644 libubb/Makefile create mode 100644 libubb/include/ubb/regbase.h create mode 100644 libubb/include/ubb/regs4740.h create mode 100644 libubb/include/ubb/ubb.h create mode 100644 libubb/ubb.c diff --git a/libubb/Makefile b/libubb/Makefile new file mode 100644 index 0000000..4820338 --- /dev/null +++ b/libubb/Makefile @@ -0,0 +1,34 @@ +# +# libubb/Makefile - Build the UBB library +# +# Written 2012 by Werner Almesberger +# Copyright 2012 Werner Almesberger +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + + +TARGET = mipsel-openwrt-linux- +CC = $(TARGET)gcc + +CFLAGS = -g -Wall -Iinclude +LIB = libubb.a + +OBJS = ubb.o +HDRS = ubb/ubb.h ubb/regbase.h ubb/regs4740.h + +.PHONY: all clean spotless + +all: $(LIB) + +$(LIB): $(OBJS) + $(AR) cr $@ $^ + +clean: + rm -f $(OBJS) + +spotless: clean + rm -f $(LIB) diff --git a/libubb/include/ubb/regbase.h b/libubb/include/ubb/regbase.h new file mode 100644 index 0000000..39f22cd --- /dev/null +++ b/libubb/include/ubb/regbase.h @@ -0,0 +1,49 @@ +/* + * include/ubb/regbase.h - Jz4740 minimum register definitions for UBB + * + * Written 2011-2012 by Werner Almesberger + * Copyright 2011-2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef UBB_REGBASE_H +#define UBB_REGBASE_H + +#include + + +#define UBB_SOC_BASE 0x10000000 +#define UBB_REG_WINDOW 0x4000000 + +#define REG(n) (*(volatile uint32_t *) (ubb_mem+(n))) + +#define REG_PADDR(r) ((unsigned long) ((void *) &(r)-ubb_mem+UBB_SOC_BASE)) + + +extern volatile void *ubb_mem; + + +#define _GPIO(n) REG(0x0010000+(n)) + +#define PDPIN _GPIO(0x300) /* port D pin level */ +#define PDDAT _GPIO(0x310) /* port D data set */ +#define PDDATS _GPIO(0x314) /* port D data set */ +#define PDDATC _GPIO(0x318) /* port D data clear */ +#define PDPULL _GPIO(0x330) /* port D pull disable */ +#define PDPULLS _GPIO(0x334) /* port D pull disable set */ +#define PDPULLC _GPIO(0x338) /* port D pull disable clear */ +#define PDFUN _GPIO(0x340) /* port D function */ +#define PDFUNS _GPIO(0x344) /* port D function set */ +#define PDFUNC _GPIO(0x348) /* port D function clear */ +#define PDSEL _GPIO(0x350) /* port D select */ +#define PDSELS _GPIO(0x354) /* port D select set */ +#define PDSELC _GPIO(0x358) /* port D select clear */ +#define PDDIR _GPIO(0x360) /* port D direction */ +#define PDDIRS _GPIO(0x364) /* port D direction set */ +#define PDDIRC _GPIO(0x368) /* port D direction clear */ + +#endif /* !UBB_REGBASE_H */ diff --git a/libubb/include/ubb/regs4740.h b/libubb/include/ubb/regs4740.h new file mode 100644 index 0000000..5c870d0 --- /dev/null +++ b/libubb/include/ubb/regs4740.h @@ -0,0 +1,75 @@ +/* + * include/ubb/regs4740.h - Jz4740 register definitions (subset) + * + * Written 2011-2012 by Werner Almesberger + * Copyright 2011-2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef UBB_REGS4740_H +#define UBB_REGS4740_H + +#include + +#include + + +#define _CGU(n) REG(0x0000000+(n)) +#define _INTC(n) REG(0x0001000+(n)) +#define _TCU(n) REG(0x0002000+(n)) +#define _MSC(n) REG(0x0021000+(n)) +#define _DMAC(n) REG(0x3020000+(n)) +#define _LCD(n) REG(0x3050000+(n)) + +#define CLKGR _CGU(0x0020) /* Clock Gate */ +#define MSCCDR _CGU(0x0068) /* MSC device clock divider */ + +#define ICMR _INTC(0x04) /* Interrupt controller mask */ +#define ICMSR _INTC(0x08) /* Interrupt controller mask set */ +#define ICMCR _INTC(0x0c) /* Interrupt controller mask clear */ + +#define TSSR _TCU(0x2c) /* Timer STOP set */ +#define TSCR _TCU(0x3c) /* Timer STOP clear */ +#define TESR _TCU(0x14) /* Timer counter enable set */ +#define TECR _TCU(0x18) /* Timer counter enable clear */ +#define TFR _TCU(0x20) /* Timer flag */ +#define TFSR _TCU(0x24) /* Timer flag set */ +#define TFCR _TCU(0x28) /* Timer flag clear */ +#define TCSR(n) _TCU(0x4c+0x10*(n)) /* Timer control */ +#define TDFR(n) _TCU(0x40+0x10*(n)) /* Timer data full */ +#define TDHR(n) _TCU(0x44+0x10*(n)) /* Timer data half */ +#define TCNT(n) _TCU(0x48+0x10*(n)) /* Timer counter */ + +#define MSC_STRPCL _MSC(0x00) /* Start/stop MMC/SD clock */ +#define MSC_STAT _MSC(0x04) /* MSC status */ +#define MSC_CLKRT _MSC(0x08) /* MSC clock rate */ +#define MSC_CMDAT _MSC(0x0c) /* MMC/SD command and data control */ +#define MSC_RESTO _MSC(0x10) /* MMC/SD response time out */ +#define MSC_BLKLEN _MSC(0x18) /* MMC/SD block length */ +#define MSC_NOB _MSC(0x1c) /* MMC/SD number of blocks */ +#define MSC_SNOB _MSC(0x20) /* MMC/SD successful blocks */ +#define MSC_IMASK _MSC(0x24) /* MMC/SD interrupt mask */ +#define MSC_IREG _MSC(0x28) /* MMC/SD interrupt */ +#define MSC_CMD _MSC(0x2c) /* MMC/SD command index */ +#define MSC_ARG _MSC(0x30) /* MMC/SD command argument */ +#define MSC_RXFIFO _MSC(0x38) /* MMC/SD receive data FIFO */ +#define MSC_TXFIFO _MSC(0x3c) /* MMC/SD transmit data FIFO */ + +#define _DMAn(n, r) _DMAC(0x20*(n)+(r)) + +#define DSA(n) _DMAn(n, 0x00) /* DMA source address */ +#define DTA(n) _DMAn(n, 0x04) /* DMA target address */ +#define DTC(n) _DMAn(n, 0x08) /* DMA transfer count */ +#define DRT(n) _DMAn(n, 0x0c) /* DMA request type */ +#define DCS(n) _DMAn(n, 0x10) /* DMA channel control/status */ +#define DCM(n) _DMAn(n, 0x14) /* DMA command */ +#define DMAC _DMAC(0x300) /* DMA control */ +#define DDR _DMAC(0x308) /* DMA doorbell */ + +#define LCDCTRL _LCD(0x30) /* LCD control */ + +#endif /* !UBB_REGS4740_H */ diff --git a/libubb/include/ubb/ubb.h b/libubb/include/ubb/ubb.h new file mode 100644 index 0000000..5079ef2 --- /dev/null +++ b/libubb/include/ubb/ubb.h @@ -0,0 +1,44 @@ +/* + * include/ubb/ubb.h - Access functions for the Universal Breakout Board + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef UBB_UBB_H +#define UBB_UBB_H + +#include + +#include + + +#define UBB_nPWR (1 << 2) /* PD02 */ +#define UBB_DAT2 (1 << 12) /* PD12 */ +#define UBB_DAT3 (1 << 13) /* PD13 */ +#define UBB_CMD (1 << 8) /* PD08 */ +#define UBB_CLK (1 << 9) /* PD09 */ +#define UBB_DAT0 (1 << 10) /* PD10 */ +#define UBB_DAT1 (1 << 11) /* PD11 */ + +#define UBB_ALL_IO (UBB_DAT2 | UBB_DAT3 | UBB_CMD | UBB_CLK | UBB_DAT0 | \ + UBB_DAT1) +#define UBB_ALL (UBB_nPWR | UBB_ALL_IO) + +#define SET(mask) PDDATS = (mask) +#define CLR(mask) PDDATC = (mask) +#define OUT(mask) PDDIRS = (mask) +#define IN(mask) PDDIRC = (mask) +#define PIN(mask) (!!(PDPIN & (mask))) + + +int ubb_open(uint32_t keep); +void ubb_power(int on); +void ubb_close(uint32_t keep); + +#endif /* !UBB_UBB_H */ diff --git a/libubb/ubb.c b/libubb/ubb.c new file mode 100644 index 0000000..bb6f58e --- /dev/null +++ b/libubb/ubb.c @@ -0,0 +1,93 @@ +/* + * libubb/ubb.c - Open/close UBB + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include +#include + +#include + + +volatile void *ubb_mem; + +static int ubb_fd; +static uint32_t data, dir, pull, func, sel; + + +static void *map(off_t addr, size_t size, int *fd) +{ + void *mem; + + *fd = open("/dev/mem", O_RDWR | O_SYNC); + if (*fd < 0) + return NULL; + mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, addr); + if (mem == MAP_FAILED) { + (void) close(*fd); + return NULL; + } + return mem; +} + + +int ubb_open(uint32_t keep) +{ + ubb_mem = map(UBB_SOC_BASE, UBB_REG_WINDOW, &ubb_fd); + if (!ubb_mem) + return -1; + + data = PDDAT & UBB_ALL; + dir = PDDIR & UBB_ALL; + pull = PDPULL & UBB_ALL; + func = PDFUN & UBB_ALL; + sel = PDSEL & UBB_ALL; + + /* enable pull-ups to let card ramp up power */ + + PDPULLC = UBB_ALL_IO & ~keep; + PDDIRC = UBB_ALL_IO & ~keep; + + PDFUNC = UBB_ALL & ~keep; + PDSELC = UBB_ALL & ~keep; + + PDDATS = UBB_nPWR & ~keep; + PDDIRS = UBB_nPWR & ~keep; + + return 0; +} + + +void ubb_power(int on) +{ + if (on) { + usleep(10*1000); + PDDATC = UBB_nPWR; + } else { + PDDATS = UBB_nPWR; + } +} + + +void ubb_close(uint32_t keep) +{ + PDSELS = sel & ~keep; + PDFUNS = func & ~keep; + + PDDATS = (data | UBB_nPWR) & ~keep; + PDDIRS = (dir | UBB_nPWR) & ~keep; + + PDPULLS = pull & ~keep; + + close(ubb_fd); +}