Root/libubb/ubb.c

1/*
2 * libubb/ubb.c - Open/close UBB
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <sys/mman.h>
18
19#include <ubb/ubb.h>
20
21
22volatile void *ubb_mem;
23
24static int ubb_fd;
25static uint32_t data, dir, pull, func, sel;
26
27
28static void *map(off_t addr, size_t size, int *fd)
29{
30    void *mem;
31
32    *fd = open("/dev/mem", O_RDWR | O_SYNC);
33    if (*fd < 0)
34        return NULL;
35    mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, addr);
36    if (mem == MAP_FAILED) {
37        (void) close(*fd);
38        return NULL;
39    }
40    return mem;
41}
42
43
44int ubb_open(uint32_t keep)
45{
46    ubb_mem = map(UBB_SOC_BASE, UBB_REG_WINDOW, &ubb_fd);
47    if (!ubb_mem)
48        return -1;
49
50    data = PDDAT & UBB_ALL;
51    dir = PDDIR & UBB_ALL;
52    pull = PDPULL & UBB_ALL;
53    func = PDFUN & UBB_ALL;
54    sel = PDSEL & UBB_ALL;
55
56    /* enable pull-ups to let card ramp up power */
57
58    PDPULLC = UBB_ALL_IO & ~keep;
59    PDDIRC = UBB_ALL_IO & ~keep;
60
61    PDFUNC = UBB_ALL & ~keep;
62    PDSELC = UBB_ALL & ~keep;
63
64    PDDATS = UBB_nPWR & ~keep;
65    PDDIRS = UBB_nPWR & ~keep;
66
67    return 0;
68}
69
70
71void ubb_power(int on)
72{
73    if (on) {
74        usleep(10*1000);
75        PDDATC = UBB_nPWR;
76    } else {
77        PDDATS = UBB_nPWR;
78    }
79}
80
81
82void ubb_close(uint32_t keep)
83{
84    PDSELS = sel & ~keep;
85    PDFUNS = func & ~keep;
86
87    PDDATS = (data | UBB_nPWR) & ~keep;
88    PDDIRS = (dir | UBB_nPWR) & ~keep;
89
90    PDPULLS = pull & ~keep;
91
92    close(ubb_fd);
93}
94

Archive Download this file

Branches:
master



interactive