lib.h (3464B)
1 /* lib.h 2 * 3 * Copyright 2011 Brian Swetland <swetland@frotz.net> 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef _FW_LIB_H_ 19 #define _FW_LIB_H_ 20 21 #include <fw/io.h> 22 #include <fw/types.h> 23 24 #define EFAIL 1 25 #define EBUSY 2 26 #define ENODEV 3 27 #define ETIMEOUT 4 28 29 #include <stdarg.h> 30 31 void board_init(void); 32 void board_debug_led(int on); 33 void reboot(void); 34 35 void vprintx(void (*_putchar_)(unsigned), const char *fmt, va_list ap); 36 void printx(const char *fmt, ...); 37 38 void serial_init(unsigned sysclk_mhz, unsigned baud); 39 void serial_putc(unsigned c); 40 41 #define GPIO_CFG_IN 0x0001 42 #define GPIO_CFG_OUT 0x0002 43 44 #define GPIO_CFG_POSITIVE 0x0001 45 #define GPIO_CFG_NEGATIVE 0x0002 46 #define GPIO_CFG_BOTH 0x0003 47 #define GPIO_CFG_LEVEL 0x0000 48 #define GPIO_CFG_EDGE 0x0004 49 50 void gpio_cfg_dir(unsigned n, unsigned dir); 51 void gpio_cfg_irq(unsigned n, unsigned cfg); 52 void gpio_set(unsigned n); 53 void gpio_clr(unsigned n); 54 int gpio_rd(unsigned n); 55 void gpio_wr(unsigned n, unsigned v); 56 int gpio_irq_check(unsigned n); 57 void gpio_irq_clear(unsigned n); 58 59 /* init USB and attach */ 60 void usb_init(unsigned vid, unsigned pid, const char *mfg_string, const char *prod_string); 61 62 /* detach from USB */ 63 void usb_stop(void); 64 65 /* read up to len bytes on bulk in 66 * - stops on a short packet 67 * - returns bytes read 68 * - returns -ENODEV if USB went offline 69 */ 70 int usb_recv(void *data, int len); 71 72 /* send len bytes on bulk out 73 * - returns bytes written 74 * - returns -ENODEV if USB went offline 75 */ 76 int usb_xmit(void *data, int len); 77 78 /* same as usb_recv but returns -ETIMEOUT 79 * if msec milliseconds pass. 80 * wait forever if msec == 0 81 */ 82 int usb_recv_timeout(void *data, int len, unsigned msec); 83 84 /* check for host, return nonzero if we found it */ 85 int usb_online(void); 86 87 88 /* low level interrupt-based USB interface */ 89 90 extern void (*usb_ep1_rx_full_cb)(void); 91 extern void (*usb_ep1_tx_empty_cb)(void); 92 extern void (*usb_ep2_rx_full_cb)(void); 93 extern void (*usb_ep2_tx_empty_cb)(void); 94 extern void (*usb_online_cb)(int online); 95 96 int usb_ep1_read(void *data, int max); 97 int usb_ep1_write(void *data, int len); 98 int usb_ep2_read(void *data, int max); 99 int usb_ep2_write(void *data, int len); 100 101 void usb_mask_ep1_rx_full(void); 102 void usb_unmask_ep1_rx_full(void); 103 void usb_mask_ep1_tx_empty(void); 104 void usb_unmask_ep1_tx_empty(void); 105 void usb_mask_ep2_rx_full(void); 106 void usb_unmask_ep2_rx_full(void); 107 void usb_mask_ep2_tx_empty(void); 108 void usb_unmask_ep2_tx_empty(void); 109 110 extern void (*usb_console_rx_cb)(u8 *buf, int len); 111 112 void usb_console_init(void); 113 114 #define ARRAY_SIZE(a) (sizeof((a))/sizeof(*(a))) 115 116 #define max(a,b) ((a) > (b) ? (a) : (b)) 117 #define min(a,b) ((a) < (b) ? (a) : (b)) 118 119 static inline unsigned clr_set_bits(unsigned val, unsigned clr, unsigned set) 120 { 121 val &= ~clr; 122 val |= set; 123 return val; 124 } 125 126 static inline void clr_set_reg(unsigned addr, unsigned clr, unsigned set) 127 { 128 unsigned val = clr_set_bits(readl(addr), clr, set); 129 writel(val, addr); 130 } 131 132 #endif 133