1228940Sdelphij/*-
2284879Sdelphij * Copyright (c) 2005-2011 HighPoint Technologies, Inc.
3228940Sdelphij * All rights reserved.
4228940Sdelphij *
5228940Sdelphij * Redistribution and use in source and binary forms, with or without
6228940Sdelphij * modification, are permitted provided that the following conditions
7228940Sdelphij * are met:
8228940Sdelphij * 1. Redistributions of source code must retain the above copyright
9228940Sdelphij *    notice, this list of conditions and the following disclaimer.
10228940Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11228940Sdelphij *    notice, this list of conditions and the following disclaimer in the
12228940Sdelphij *    documentation and/or other materials provided with the distribution.
13228940Sdelphij *
14228940Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15228940Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16228940Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17228940Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18228940Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19228940Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20228940Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21228940Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22228940Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23228940Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24228940Sdelphij * SUCH DAMAGE.
25228940Sdelphij *
26228940Sdelphij * $FreeBSD: releng/10.3/sys/dev/hpt27xx/osm.h 284879 2015-06-26 19:55:01Z delphij $
27228940Sdelphij */
28228940Sdelphij
29228940Sdelphij#include <dev/hpt27xx/hpt27xx_config.h>
30228940Sdelphij
31228940Sdelphij#ifndef _HPT_OSM_H_
32228940Sdelphij#define _HPT_OSM_H_
33228940Sdelphij
34228940Sdelphij#define VERMAGIC_OSM 6
35228940Sdelphij
36228940Sdelphij
37228940Sdelphij#define MAX_MEMBERS 64
38228940Sdelphij
39228940Sdelphij#define os_max_queue_comm 32
40228940Sdelphij#define os_max_sg_descriptors 18
41228940Sdelphij
42228940Sdelphij
43228940Sdelphijextern int os_max_cache_size;
44228940Sdelphij
45228940Sdelphij
46228940Sdelphij#define DMAPOOL_PAGE_SIZE 0x1000 /* PAGE_SIZE (i386/x86_64) */
47228940Sdelphij#define os_max_cache_pages (os_max_cache_size/DMAPOOL_PAGE_SIZE)
48228940Sdelphij
49228940Sdelphij/* data types */
50228940Sdelphijtypedef unsigned int HPT_UINT, HPT_U32;
51228940Sdelphijtypedef unsigned long HPT_UPTR;
52228940Sdelphijtypedef unsigned short HPT_U16;
53228940Sdelphijtypedef unsigned char HPT_U8;
54228940Sdelphijtypedef unsigned long HPT_TIME;
55228940Sdelphijtypedef unsigned long long HPT_U64;
56284879Sdelphijtypedef long long HPT_64;
57228940Sdelphij
58228940Sdelphij#define CPU_TO_LE64(x) (x)
59228940Sdelphij#define CPU_TO_LE32(x) (x)
60228940Sdelphij#define CPU_TO_LE16(x) (x)
61228940Sdelphij#define LE32_TO_CPU(x) (x)
62228940Sdelphij#define LE16_TO_CPU(x) (x)
63228940Sdelphij#define LE64_TO_CPU(x) (x)
64228940Sdelphij
65228940Sdelphijstatic __inline HPT_U64 CPU_TO_BE64(HPT_U64 x)
66228940Sdelphij{
67228940Sdelphij	HPT_U8 *p = (HPT_U8 *)&x;
68228940Sdelphij	return ((HPT_U64)p[0] << 56) |
69228940Sdelphij		((HPT_U64)p[1] << 48) |
70228940Sdelphij		((HPT_U64)p[2] << 40) |
71228940Sdelphij		((HPT_U64)p[3] << 32) |
72228940Sdelphij		((HPT_U64)p[4] << 24) |
73228940Sdelphij		((HPT_U64)p[5] << 16) |
74228940Sdelphij		((HPT_U64)p[6] << 8) |
75228940Sdelphij		p[7];
76228940Sdelphij}
77228940Sdelphij
78228940Sdelphijstatic __inline HPT_U32 CPU_TO_BE32(HPT_U32 x)
79228940Sdelphij{
80228940Sdelphij	HPT_U8 *p = (HPT_U8 *)&x;
81228940Sdelphij	return ((HPT_U32)p[0] << 24) |
82228940Sdelphij		((HPT_U32)p[1] << 16) |
83228940Sdelphij		((HPT_U32)p[2] << 8) | p[3];
84228940Sdelphij}
85228940Sdelphij
86228940Sdelphijstatic __inline HPT_U16 CPU_TO_BE16(HPT_U16 x)
87228940Sdelphij{
88228940Sdelphij	return ((HPT_U8)x << 8) | (x>>8);
89228940Sdelphij}
90228940Sdelphij
91228940Sdelphij#define BE16_TO_CPU(x) CPU_TO_BE16(x)
92228940Sdelphij#define BE32_TO_CPU(x) CPU_TO_BE32(x)
93228940Sdelphij#define BE64_TO_CPU(x) CPU_TO_BE64(x)
94228940Sdelphij
95228940Sdelphij#define FAR
96228940Sdelphij#define EXTERN_C
97228940Sdelphij
98228940Sdelphijtypedef void * HPT_PTR;
99228940Sdelphij
100228940Sdelphijtypedef HPT_U64 HPT_LBA;
101228940Sdelphijtypedef HPT_U64 HPT_RAW_LBA;
102228940Sdelphij#define MAX_LBA_VALUE 0xffffffffffffffffull
103228940Sdelphij#define MAX_RAW_LBA_VALUE MAX_LBA_VALUE
104228940Sdelphij#define RAW_LBA(x) (x)
105228940Sdelphij#define LO_LBA(x) ((HPT_U32)(x))
106228940Sdelphij#define HI_LBA(x) (sizeof(HPT_LBA)>4? (HPT_U32)((x)>>32) : 0)
107228940Sdelphij#define LBA_FORMAT_STR "0x%llX"
108228940Sdelphij
109228940Sdelphijtypedef HPT_U64 BUS_ADDRESS;
110228940Sdelphij#define LO_BUSADDR(x) ((HPT_U32)(x))
111228940Sdelphij#define HI_BUSADDR(x) (sizeof(BUS_ADDRESS)>4? (x)>>32 : 0)
112228940Sdelphij
113228940Sdelphijtypedef unsigned char HPT_BOOL;
114228940Sdelphij#define HPT_TRUE  1
115228940Sdelphij#define HPT_FALSE 0
116228940Sdelphij
117228940Sdelphijtypedef struct _TIME_RECORD {
118228940Sdelphij   HPT_U32        seconds:6;      /* 0 - 59 */
119228940Sdelphij   HPT_U32        minutes:6;      /* 0 - 59 */
120228940Sdelphij   HPT_U32        month:4;        /* 1 - 12 */
121228940Sdelphij   HPT_U32        hours:6;        /* 0 - 59 */
122228940Sdelphij   HPT_U32        day:5;          /* 1 - 31 */
123228940Sdelphij   HPT_U32        year:5;         /* 0=2000, 31=2031 */
124228940Sdelphij} TIME_RECORD;
125228940Sdelphij
126228940Sdelphij/* hardware access */
127228940SdelphijHPT_U8   os_inb  (void *port);
128228940SdelphijHPT_U16  os_inw  (void *port);
129228940SdelphijHPT_U32  os_inl  (void *port);
130228940Sdelphijvoid     os_outb (void *port, HPT_U8 value);
131228940Sdelphijvoid     os_outw (void *port, HPT_U16 value);
132228940Sdelphijvoid     os_outl (void *port, HPT_U32 value);
133228940Sdelphijvoid     os_insw (void *port, HPT_U16 *buffer, HPT_U32 count);
134228940Sdelphijvoid     os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count);
135228940Sdelphij
136228940Sdelphijextern HPT_U32 __dummy_reg; /* to avoid the compiler warning */
137228940Sdelphij
138228940Sdelphij#define os_readb(addr) (*(HPT_U8 *)&__dummy_reg = *(volatile HPT_U8 *)(addr))
139228940Sdelphij#define os_readw(addr) (*(HPT_U16 *)&__dummy_reg = *(volatile HPT_U16 *)(addr))
140228940Sdelphij#define os_readl(addr) (*(HPT_U32 *)&__dummy_reg = *(volatile HPT_U32 *)(addr))
141228940Sdelphij
142228940Sdelphij#define os_writeb(addr, val) *(volatile HPT_U8 *)(addr) = (HPT_U8)(val)
143228940Sdelphij#define os_writew(addr, val) *(volatile HPT_U16 *)(addr) = (HPT_U16)(val)
144228940Sdelphij#define os_writel(addr, val) *(volatile HPT_U32 *)(addr) = (HPT_U32)(val)
145228940Sdelphij
146228940Sdelphij/* PCI configuration space for specified device*/
147228940SdelphijHPT_U8   os_pci_readb (void *osext, HPT_U8 offset);
148228940SdelphijHPT_U16  os_pci_readw (void *osext, HPT_U8 offset);
149228940SdelphijHPT_U32  os_pci_readl (void *osext, HPT_U8 offset);
150228940Sdelphijvoid     os_pci_writeb(void *osext, HPT_U8 offset, HPT_U8 value);
151228940Sdelphijvoid     os_pci_writew(void *osext, HPT_U8 offset, HPT_U16 value);
152228940Sdelphijvoid     os_pci_writel(void *osext, HPT_U8 offset, HPT_U32 value);
153228940Sdelphij
154228940Sdelphij/* obsolute interface */
155228940Sdelphij#define MAX_PCI_BUS_NUMBER 0xff
156228940Sdelphij#define MAX_PCI_DEVICE_NUMBER 32
157228940Sdelphij#define MAX_PCI_FUNC_NUMBER 1
158228940SdelphijHPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
159228940SdelphijHPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
160228940Sdelphijvoid pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v);
161228940Sdelphijvoid pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v);
162228940Sdelphij
163228940Sdelphij
164228940Sdelphijvoid *os_map_pci_bar(
165228940Sdelphij	void *osext,
166228940Sdelphij	int index,
167228940Sdelphij	HPT_U32 offset,
168228940Sdelphij	HPT_U32 length
169228940Sdelphij);
170228940Sdelphij
171228940Sdelphij
172228940Sdelphijvoid os_unmap_pci_bar(void *osext, void *base);
173228940Sdelphij
174228940Sdelphij#define os_kmap_sgptr(psg) (psg->addr._logical)
175228940Sdelphij#define os_kunmap_sgptr(ptr)
176228940Sdelphij#define os_set_sgptr(psg, ptr) (psg)->addr._logical = (ptr)
177228940Sdelphij
178228940Sdelphij/* timer */
179228940Sdelphijvoid *os_add_timer(void *osext, HPT_U32 microseconds, void (*proc)(void *), void *arg);
180228940Sdelphijvoid  os_del_timer(void *handle);
181228940Sdelphijvoid  os_request_timer(void * osext, HPT_U32 interval);
182228940SdelphijHPT_TIME os_query_time(void);
183228940Sdelphij
184228940Sdelphij/* task */
185228940Sdelphij#define OS_SUPPORT_TASK
186228940Sdelphij
187228940Sdelphijtypedef struct _OSM_TASK {
188228940Sdelphij	struct _OSM_TASK *next;
189228940Sdelphij	void (*func)(void *vbus, void *data);
190228940Sdelphij	void *data;
191228940Sdelphij}
192228940SdelphijOSM_TASK;
193228940Sdelphij
194228940Sdelphijvoid os_schedule_task(void *osext, OSM_TASK *task);
195228940Sdelphij
196228940Sdelphij/* misc */
197228940SdelphijHPT_U32 os_get_stamp(void);
198228940Sdelphijvoid os_stallexec(HPT_U32 microseconds);
199228940Sdelphij
200228940Sdelphij#ifndef _SYS_LIBKERN_H_
201228940Sdelphij#define memcpy(dst, src, size) __builtin_memcpy((dst), (src), (size))
202228940Sdelphij#define memcmp(dst, src, size) __builtin_memcmp((dst), (src), (size))
203228940Sdelphij#define strcpy(dst, src) __builtin_strcpy((dst), (src))
204228940Sdelphijstatic __inline void * memset(void *dst, int c, unsigned long size)
205228940Sdelphij{
206228940Sdelphij	char *p;
207228940Sdelphij	for (p=(char*)dst; size; size--,p++) *p = c;
208228940Sdelphij	return dst;
209228940Sdelphij}
210228940Sdelphij#endif
211228940Sdelphij
212228940Sdelphij#define farMemoryCopy(a,b,c) memcpy((char *)(a), (char *)(b), (HPT_U32)c)
213228940Sdelphij
214228940Sdelphij
215228940Sdelphij#define os_register_device(osext, target_id)
216228940Sdelphij#define os_unregister_device(osext, target_id)
217228940Sdelphijint os_query_remove_device(void *osext, int target_id);
218228940Sdelphijint os_revalidate_device(void *osext, int target_id);
219228940Sdelphij
220228940SdelphijHPT_U8 os_get_vbus_seq(void *osext);
221228940Sdelphij
222228940Sdelphij/* debug support */
223228940Sdelphijint  os_printk(char *fmt, ...);
224228940Sdelphij
225228940Sdelphij#if DBG
226228940Sdelphijextern int hpt_dbg_level;
227284879Sdelphij#define dbg_printk(fmt, args...) \
228284879Sdelphij	do {\
229284879Sdelphij		char *__p = fmt;\
230284879Sdelphij		int __level;\
231284879Sdelphij		if (__p[0]=='<' && __p[2]=='>') {\
232284879Sdelphij			__level = __p[1] - '0';\
233284879Sdelphij			__p += 3;\
234284879Sdelphij		} else\
235284879Sdelphij			__level = 7;\
236284879Sdelphij		if (hpt_dbg_level >= __level)\
237284879Sdelphij			os_printk(__p, ##args);\
238284879Sdelphij	} while (0)
239284879Sdelphij
240284879Sdelphij#define KdPrint(x)  do { dbg_printk x; } while (0)
241228940Sdelphijvoid __os_dbgbreak(const char *file, int line);
242228940Sdelphij#define os_dbgbreak() __os_dbgbreak(__FILE__, __LINE__)
243228940Sdelphij#define HPT_ASSERT(x) do { if (!(x)) os_dbgbreak(); } while (0)
244228940Sdelphijvoid os_check_stack(const char *location, int size);
245228940Sdelphij#define HPT_CHECK_STACK(size) os_check_stack(__FUNCTION__, (size))
246228940Sdelphij#else
247228940Sdelphij#define KdPrint(x)
248228940Sdelphij#define HPT_ASSERT(x)
249228940Sdelphij#define HPT_CHECK_STACK(size)
250228940Sdelphij#endif
251228940Sdelphij
252228940Sdelphij#define OsPrint(x) do { os_printk x; } while (0)
253228940Sdelphij
254228940Sdelphij#endif
255