2 * Copyright (c) 2011 HighPoint Technologies, Inc.
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
29 #include <dev/hpt27xx/hpt27xx_config.h>
31 #include <dev/hpt27xx/os_bsd.h>
34 HPT_U8 os_inb (void *port) { return inb((unsigned)(HPT_UPTR)port); }
35 HPT_U16 os_inw (void *port) { return inw((unsigned)(HPT_UPTR)port); }
36 HPT_U32 os_inl (void *port) { return inl((unsigned)(HPT_UPTR)port); }
38 void os_outb (void *port, HPT_U8 value) { outb((unsigned)(HPT_UPTR)port, (value)); }
39 void os_outw (void *port, HPT_U16 value) { outw((unsigned)(HPT_UPTR)port, (value)); }
40 void os_outl (void *port, HPT_U32 value) { outl((unsigned)(HPT_UPTR)port, (value)); }
42 void os_insw (void *port, HPT_U16 *buffer, HPT_U32 count)
43 { insw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
45 void os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count)
46 { outsw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
48 HPT_U32 __dummy_reg = 0;
50 /* PCI configuration space */
51 HPT_U8 os_pci_readb (void *osext, HPT_U8 offset)
53 return pci_read_config(((PHBA)osext)->pcidev, offset, 1);
56 HPT_U16 os_pci_readw (void *osext, HPT_U8 offset)
58 return pci_read_config(((PHBA)osext)->pcidev, offset, 2);
61 HPT_U32 os_pci_readl (void *osext, HPT_U8 offset)
63 return pci_read_config(((PHBA)osext)->pcidev, offset, 4);
66 void os_pci_writeb (void *osext, HPT_U8 offset, HPT_U8 value)
68 pci_write_config(((PHBA)osext)->pcidev, offset, value, 1);
71 void os_pci_writew (void *osext, HPT_U8 offset, HPT_U16 value)
73 pci_write_config(((PHBA)osext)->pcidev, offset, value, 2);
76 void os_pci_writel (void *osext, HPT_U8 offset, HPT_U32 value)
78 pci_write_config(((PHBA)osext)->pcidev, offset, value, 4);
81 #if __FreeBSD_version < 500043
82 /* PCI space access */
83 HPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg)
92 v = pci_cfgread(&pciref, reg, 1);
95 HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg)
104 v = pci_cfgread(&pciref, reg, 4);
107 void pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v)
116 pci_cfgwrite(&pciref, reg, v, 1);
118 void pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v)
127 pci_cfgwrite(&pciref, reg, v, 4);
128 }/* PCI space access */
130 HPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg)
132 return (HPT_U8)pci_cfgregread(bus, dev, func, reg, 1);
134 HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg)
136 return (HPT_U32)pci_cfgregread(bus, dev, func, reg, 4);
138 void pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v)
140 pci_cfgregwrite(bus, dev, func, reg, v, 1);
142 void pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v)
144 pci_cfgregwrite(bus, dev, func, reg, v, 4);
145 }/* PCI space access */
148 void *os_map_pci_bar(
155 PHBA hba = (PHBA)osext;
158 hba->pcibar[index].rid = 0x10 + index * 4;
159 base = pci_read_config(hba->pcidev, hba->pcibar[index].rid, 4);
162 hba->pcibar[index].type = SYS_RES_IOPORT;
163 hba->pcibar[index].res = bus_alloc_resource(hba->pcidev,
164 hba->pcibar[index].type, &hba->pcibar[index].rid, 0, ~0, length, RF_ACTIVE);
165 hba->pcibar[index].base = (void *)(unsigned long)(base & ~0x1);
167 hba->pcibar[index].type = SYS_RES_MEMORY;
168 hba->pcibar[index].res = bus_alloc_resource(hba->pcidev,
169 hba->pcibar[index].type, &hba->pcibar[index].rid, 0, ~0, length, RF_ACTIVE);
170 hba->pcibar[index].base = (char *)rman_get_virtual(hba->pcibar[index].res) + offset;
173 return hba->pcibar[index].base;
176 void os_unmap_pci_bar(void *osext, void *base)
178 PHBA hba = (PHBA)osext;
181 for (index=0; index<6; index++) {
182 if (hba->pcibar[index].base==base) {
183 bus_release_resource(hba->pcidev, hba->pcibar[index].type,
184 hba->pcibar[index].rid, hba->pcibar[index].res);
185 hba->pcibar[index].base = 0;
191 void freelist_reserve(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT count)
193 PVBUS_EXT vbus_ext = osext;
195 if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
196 vbus_ext = ((PHBA)osext)->vbus_ext;
198 list->next = vbus_ext->freelist_head;
199 vbus_ext->freelist_head = list;
204 list->reserved_count =
209 void *freelist_get(struct freelist *list)
213 HPT_ASSERT(list->head);
215 list->head = *(void **)result;
222 void freelist_put(struct freelist * list, void *p)
224 HPT_ASSERT(list->dma==0);
226 *(void **)p = list->head;
230 void freelist_reserve_dma(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT alignment, HPT_UINT count)
232 PVBUS_EXT vbus_ext = osext;
234 if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
235 vbus_ext = ((PHBA)osext)->vbus_ext;
237 list->next = vbus_ext->freelist_dma_head;
238 vbus_ext->freelist_dma_head = list;
240 list->alignment = alignment;
244 list->reserved_count =
249 void *freelist_get_dma(struct freelist *list, BUS_ADDRESS *busaddr)
252 HPT_ASSERT(list->dma);
253 result = freelist_get(list);
255 *busaddr = *(BUS_ADDRESS *)((void **)result+1);
259 void freelist_put_dma(struct freelist *list, void *p, BUS_ADDRESS busaddr)
261 HPT_ASSERT(list->dma);
263 *(void **)p = list->head;
264 *(BUS_ADDRESS *)((void **)p+1) = busaddr;
268 HPT_U32 os_get_stamp(void)
271 do { stamp = random(); } while (stamp==0);
275 void os_stallexec(HPT_U32 microseconds)
280 static void os_timer_for_ldm(void *arg)
282 PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
283 ldm_on_timer((PVBUS)vbus_ext->vbus);
286 void os_request_timer(void * osext, HPT_U32 interval)
288 PVBUS_EXT vbus_ext = osext;
290 HPT_ASSERT(vbus_ext->ext_type==EXT_TYPE_VBUS);
292 untimeout(os_timer_for_ldm, vbus_ext, vbus_ext->timer);
293 vbus_ext->timer = timeout(os_timer_for_ldm, vbus_ext, interval * hz / 1000000);
296 HPT_TIME os_query_time(void)
298 return ticks * (1000000 / hz);
301 void os_schedule_task(void *osext, OSM_TASK *task)
303 PVBUS_EXT vbus_ext = osext;
305 HPT_ASSERT(task->next==0);
307 if (vbus_ext->tasks==0)
308 vbus_ext->tasks = task;
310 OSM_TASK *t = vbus_ext->tasks;
311 while (t->next) t = t->next;
315 if (vbus_ext->worker.ta_context)
316 TASK_ENQUEUE(&vbus_ext->worker);
319 int os_revalidate_device(void *osext, int id)
325 int os_query_remove_device(void *osext, int id)
327 PVBUS_EXT vbus_ext = (PVBUS_EXT)osext;
328 struct cam_periph *periph = NULL;
329 struct cam_path *path;
330 int status,retval = 0;
332 status = xpt_create_path(&path, NULL, vbus_ext->sim->path_id, id, 0);
333 if (status == CAM_REQ_CMP) {
334 if((periph = cam_periph_find(path, "da")) != NULL){
335 if(periph->refcount >= 1)
344 HPT_U8 os_get_vbus_seq(void *osext)
346 return ((PVBUS_EXT)osext)->sim->path_id;
349 int os_printk(char *fmt, ...)
352 static char buf[512];
355 vsnprintf(buf, sizeof(buf), fmt, args);
357 return printf("%s: %s\n", driver_name, buf);
361 void os_check_stack(const char *location, int size){}
363 void __os_dbgbreak(const char *file, int line)
365 printf("*** break at %s:%d ***", file, line);
369 int hpt_dbg_level = 1;