2 * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
34 #include "api_public.h"
38 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
40 #define debugf(fmt, args...)
43 /* Some random address used by U-Boot. */
44 extern long uboot_address;
47 valid_sig(struct api_signature *sig)
50 struct api_signature s;
55 * Clear the checksum field (in the local copy) so as to calculate the
56 * CRC with the same initial contents as at the time when the sig was
62 checksum = crc32((void *)&s, sizeof(struct api_signature));
64 if (checksum != sig->checksum)
71 * Searches for the U-Boot API signature
73 * returns 1/0 depending on found/not found result
76 api_search_sig(struct api_signature **sig)
78 unsigned char *sp, *spend;
83 if (uboot_address == 0)
84 uboot_address = 255 * 1024 * 1024;
86 sp = (void *)(uboot_address & ~0x000fffff);
87 spend = sp + 0x00300000 - API_SIG_MAGLEN;
89 if (!bcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
90 *sig = (struct api_signature *)sp;
101 /****************************************
105 ****************************************/
112 if (!syscall(API_GETC, NULL, (uint32_t)&c))
123 if (!syscall(API_TSTC, NULL, (uint32_t)&t))
133 syscall(API_PUTC, NULL, (uint32_t)&c);
137 ub_puts(const char *s)
140 syscall(API_PUTS, NULL, (uint32_t)s);
143 /****************************************
147 ****************************************/
153 syscall(API_RESET, NULL);
156 static struct mem_region mr[UB_MAX_MR];
157 static struct sys_info si;
160 ub_get_sys_info(void)
164 memset(&si, 0, sizeof(struct sys_info));
166 si.mr_no = UB_MAX_MR;
167 memset(&mr, 0, sizeof(mr));
169 if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
172 return ((err) ? NULL : &si);
175 /****************************************
179 ****************************************/
182 ub_udelay(unsigned long usec)
185 syscall(API_UDELAY, NULL, &usec);
189 ub_get_timer(unsigned long base)
193 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
199 /****************************************************************************
203 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
205 ***************************************************************************/
207 static struct device_info devices[UB_MAX_DEV];
213 return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
217 * Enumerates the devices: fills out device_info elements in the devices[]
220 * returns: number of devices found
225 struct device_info *di;
228 memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
231 if (!syscall(API_DEV_ENUM, NULL, di))
234 while (di->cookie != NULL) {
236 if (++n >= UB_MAX_DEV)
239 /* take another device_info */
242 /* pass on the previous cookie */
243 di->cookie = devices[n - 1].cookie;
245 if (!syscall(API_DEV_ENUM, NULL, di))
253 * handle: 0-based id of the device
255 * returns: 0 when OK, err otherwise
258 ub_dev_open(int handle)
260 struct device_info *di;
263 if (handle < 0 || handle >= UB_MAX_DEV)
266 di = &devices[handle];
267 if (!syscall(API_DEV_OPEN, &err, di))
274 ub_dev_close(int handle)
276 struct device_info *di;
278 if (handle < 0 || handle >= UB_MAX_DEV)
281 di = &devices[handle];
282 if (!syscall(API_DEV_CLOSE, NULL, di))
289 * Validates device for read/write, it has to:
294 * returns: 0/1 accordingly
297 dev_valid(int handle)
300 if (handle < 0 || handle >= UB_MAX_DEV)
303 if (devices[handle].state != DEV_STA_OPEN)
310 dev_stor_valid(int handle)
313 if (!dev_valid(handle))
316 if (!(devices[handle].type & DEV_TYP_STOR))
323 ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
326 struct device_info *di;
330 if (!dev_stor_valid(handle))
333 di = &devices[handle];
334 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
344 dev_net_valid(int handle)
347 if (!dev_valid(handle))
350 if (devices[handle].type != DEV_TYP_NET)
357 ub_dev_recv(int handle, void *buf, int len, int *rlen)
359 struct device_info *di;
360 int err = 0, act_len;
362 if (!dev_net_valid(handle))
365 di = &devices[handle];
366 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
376 ub_dev_send(int handle, void *buf, int len)
378 struct device_info *di;
381 if (!dev_net_valid(handle))
384 di = &devices[handle];
385 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
392 ub_stor_type(int type)
395 if (type & DT_STOR_IDE)
398 if (type & DT_STOR_SCSI)
401 if (type & DT_STOR_USB)
404 if (type & DT_STOR_MMC)
407 if (type & DT_STOR_SATA)
414 ub_mem_type(int flags)
417 switch (flags & 0x000F) {
430 ub_dump_di(int handle)
432 struct device_info *di = ub_dev_get(handle);
435 printf("device info (%d):\n", handle);
436 printf(" cookie\t= 0x%08x\n", (uint32_t)di->cookie);
437 printf(" type\t\t= 0x%08x\n", di->type);
439 if (di->type == DEV_TYP_NET) {
440 printf(" hwaddr\t= ");
441 for (i = 0; i < 6; i++)
442 printf("%02x ", di->di_net.hwaddr[i]);
446 } else if (di->type & DEV_TYP_STOR) {
447 printf(" type\t\t= %s\n", ub_stor_type(di->type));
448 printf(" blk size\t\t= %ld\n", di->di_stor.block_size);
449 printf(" blk count\t\t= %ld\n", di->di_stor.block_count);
454 ub_dump_si(struct sys_info *si)
458 printf("sys info:\n");
459 printf(" clkbus\t= %ld MHz\n", si->clk_bus / 1000 / 1000);
460 printf(" clkcpu\t= %ld MHz\n", si->clk_cpu / 1000 / 1000);
461 printf(" bar\t\t= 0x%08lx\n", si->bar);
464 for (i = 0; i < si->mr_no; i++) {
465 if (si->mr[i].flags == 0)
468 printf(" start\t= 0x%08lx\n", si->mr[i].start);
469 printf(" size\t= 0x%08lx\n", si->mr[i].size);
470 printf(" type\t= %s\n", ub_mem_type(si->mr[i].flags));
475 /****************************************
479 ****************************************/
482 ub_env_get(const char *name)
486 if (!syscall(API_ENV_GET, NULL, (uint32_t)name, (uint32_t)&value))
493 ub_env_set(const char *name, char *value)
496 syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value);
499 static char env_name[256];
502 ub_env_enum(const char *last)
504 const char *env, *str;
508 * It's OK to pass only the name piece as last (and not the whole
509 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
510 * internally, which handles such case
513 if (!syscall(API_ENV_ENUM, NULL, (uint32_t)last, (uint32_t)&env))
517 /* no more env. variables to enumerate */
520 /* next enumerated env var */
521 memset(env_name, 0, 256);
522 for (i = 0, str = env; *str != '=' && *str != '\0';)
523 env_name[i++] = *str++;