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"
41 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
43 #define debugf(fmt, args...)
46 /* Some random address used by U-Boot. */
47 extern long uboot_address;
50 valid_sig(struct api_signature *sig)
53 struct api_signature s;
58 * Clear the checksum field (in the local copy) so as to calculate the
59 * CRC with the same initial contents as at the time when the sig was
65 checksum = crc32((void *)&s, sizeof(struct api_signature));
67 if (checksum != sig->checksum)
74 * Searches for the U-Boot API signature
76 * returns 1/0 depending on found/not found result
79 api_search_sig(struct api_signature **sig)
81 unsigned char *sp, *spend;
86 if (uboot_address == 0)
87 uboot_address = 255 * 1024 * 1024;
89 sp = (void *)(uboot_address & ~0x000fffff);
90 spend = sp + 0x00300000 - API_SIG_MAGLEN;
92 if (!bcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
93 *sig = (struct api_signature *)sp;
104 /****************************************
108 ****************************************/
115 if (!syscall(API_GETC, NULL, (uint32_t)&c))
126 if (!syscall(API_TSTC, NULL, (uint32_t)&t))
136 syscall(API_PUTC, NULL, (uint32_t)&c);
140 ub_puts(const char *s)
143 syscall(API_PUTS, NULL, (uint32_t)s);
146 /****************************************
150 ****************************************/
156 syscall(API_RESET, NULL);
159 static struct mem_region mr[UB_MAX_MR];
160 static struct sys_info si;
163 ub_get_sys_info(void)
167 memset(&si, 0, sizeof(struct sys_info));
169 si.mr_no = UB_MAX_MR;
170 memset(&mr, 0, sizeof(mr));
172 if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
175 return ((err) ? NULL : &si);
178 /****************************************
182 ****************************************/
185 ub_udelay(unsigned long usec)
188 syscall(API_UDELAY, NULL, &usec);
192 ub_get_timer(unsigned long base)
196 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
202 /****************************************************************************
206 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
208 ***************************************************************************/
210 static struct device_info devices[UB_MAX_DEV];
216 return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
220 * Enumerates the devices: fills out device_info elements in the devices[]
223 * returns: number of devices found
228 struct device_info *di;
231 memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
234 if (!syscall(API_DEV_ENUM, NULL, di))
237 while (di->cookie != NULL) {
239 if (++n >= UB_MAX_DEV)
242 /* take another device_info */
245 /* pass on the previous cookie */
246 di->cookie = devices[n - 1].cookie;
248 if (!syscall(API_DEV_ENUM, NULL, di))
256 * handle: 0-based id of the device
258 * returns: 0 when OK, err otherwise
261 ub_dev_open(int handle)
263 struct device_info *di;
266 if (handle < 0 || handle >= UB_MAX_DEV)
269 di = &devices[handle];
270 if (!syscall(API_DEV_OPEN, &err, di))
277 ub_dev_close(int handle)
279 struct device_info *di;
281 if (handle < 0 || handle >= UB_MAX_DEV)
284 di = &devices[handle];
285 if (!syscall(API_DEV_CLOSE, NULL, di))
292 * Validates device for read/write, it has to:
297 * returns: 0/1 accordingly
300 dev_valid(int handle)
303 if (handle < 0 || handle >= UB_MAX_DEV)
306 if (devices[handle].state != DEV_STA_OPEN)
313 dev_stor_valid(int handle)
316 if (!dev_valid(handle))
319 if (!(devices[handle].type & DEV_TYP_STOR))
326 ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
329 struct device_info *di;
333 if (!dev_stor_valid(handle))
336 di = &devices[handle];
337 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
347 dev_net_valid(int handle)
350 if (!dev_valid(handle))
353 if (devices[handle].type != DEV_TYP_NET)
360 ub_dev_recv(int handle, void *buf, int len, int *rlen)
362 struct device_info *di;
363 int err = 0, act_len;
365 if (!dev_net_valid(handle))
368 di = &devices[handle];
369 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
379 ub_dev_send(int handle, void *buf, int len)
381 struct device_info *di;
384 if (!dev_net_valid(handle))
387 di = &devices[handle];
388 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
395 ub_stor_type(int type)
398 if (type & DT_STOR_IDE)
401 if (type & DT_STOR_SCSI)
404 if (type & DT_STOR_USB)
407 if (type & DT_STOR_MMC)
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++;