1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12#pragma once
13
14#include <stdlib.h>
15#include <platsupport/io.h>
16#include <platsupport/delay.h>
17#include <utils/ansi.h>
18#include <utils/util.h>
19#include <utils/zf_log.h>
20#include <usb/usb_host.h>
21
22void otg_irq(void);
23
24#define MAP_DEVICE(o, p, s) ps_io_map(&o->io_mapper, p, s, 0, PS_MEM_NORMAL)
25#define GET_RESOURCE(ops, id) MAP_DEVICE(ops, id##_PADDR, id##_SIZE)
26
27extern ps_malloc_ops_t *ps_malloc_ops;
28static inline void *usb_malloc(size_t size)
29{
30	int ret;
31
32	if (ps_malloc_ops && ps_malloc_ops->calloc) {
33		void *ptr;
34		ret = ps_calloc(ps_malloc_ops, 1, size, &ptr);
35		if (!ret) {
36			ZF_LOGF("Malloc error\n");
37		}
38		return ptr;
39	} else {
40		return calloc(1, size);
41	}
42}
43
44static inline void usb_free(void *ptr)
45{
46	int ret;
47
48	if (ps_malloc_ops && ps_malloc_ops->free) {
49		ret = ps_free(ps_malloc_ops, 1, ptr);
50		if (!ret) {
51			ZF_LOGF("Free error\n");
52		}
53	} else {
54		free(ptr);
55	}
56}
57
58static inline void dsb()
59{
60#ifdef ARCH_ARM
61	asm volatile("dsb");
62#else
63	asm volatile ("" ::: "memory");
64#endif
65}
66
67static inline void *ps_dma_alloc_pinned(ps_dma_man_t * dma_man, size_t size,
68					int align, int cache,
69					ps_mem_flags_t flags, uintptr_t * paddr)
70{
71	void *addr;
72	if (!dma_man) {
73		ZF_LOGF("Invalid arguments\n");
74	}
75	addr = ps_dma_alloc(dma_man, size, align, cache, flags);
76	if (addr != NULL) {
77		*paddr = ps_dma_pin(dma_man, addr, size);
78	}
79	return addr;
80}
81
82static inline void
83ps_dma_free_pinned(ps_dma_man_t * dma_man, void *addr, size_t size)
84{
85	if (!dma_man) {
86		ZF_LOGF("Invalid arguments\n");
87	}
88	ps_dma_unpin(dma_man, addr, size);
89	ps_dma_free(dma_man, addr, size);
90}
91
92