17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*19397407SSherry Moore * Common Development and Distribution License (the "License"). 6*19397407SSherry Moore * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*19397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23f47a9c50Smathue * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/types.h> 287c478bd9Sstevel@tonic-gate #include <sys/time.h> 297c478bd9Sstevel@tonic-gate #include <sys/errno.h> 307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 317c478bd9Sstevel@tonic-gate #include <sys/param.h> 327c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 337c478bd9Sstevel@tonic-gate #include <sys/conf.h> 347c478bd9Sstevel@tonic-gate #include <sys/open.h> 357c478bd9Sstevel@tonic-gate #include <sys/stat.h> 367c478bd9Sstevel@tonic-gate #include <sys/clock.h> 377c478bd9Sstevel@tonic-gate #include <sys/gpio_87317.h> 387c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 407c478bd9Sstevel@tonic-gate #include <sys/file.h> 417c478bd9Sstevel@tonic-gate #ifdef DEBUG 427c478bd9Sstevel@tonic-gate #include <sys/promif.h> 437c478bd9Sstevel@tonic-gate #endif 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* a non zero value causes debug info to be displayed */ 477c478bd9Sstevel@tonic-gate uint_t gpio_debug_flag = 0; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #ifdef DEBUG 517c478bd9Sstevel@tonic-gate static void gpio_debug(dev_info_t *dip, char *format, uint_t arg1, uint_t arg2, 527c478bd9Sstevel@tonic-gate uint_t arg3, uint_t arg4, uint_t arg5); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #define DBG(dip, format, arg1, arg2, arg3, arg4, arg5) \ 557c478bd9Sstevel@tonic-gate gpio_debug(dip, format, (uint_t)arg1, (uint_t)arg2, (uint_t)arg3, \ 567c478bd9Sstevel@tonic-gate (uint_t)arg4, (uint_t)arg5) 577c478bd9Sstevel@tonic-gate #else 587c478bd9Sstevel@tonic-gate #define DBG(dip, format, arg1, arg2, arg3, arg4, arg5) 597c478bd9Sstevel@tonic-gate #endif 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate /* Driver soft state structure */ 637c478bd9Sstevel@tonic-gate struct gpio_softc { 647c478bd9Sstevel@tonic-gate dev_info_t *gp_dip; 657c478bd9Sstevel@tonic-gate kmutex_t gp_mutex; 667c478bd9Sstevel@tonic-gate int gp_state; 677c478bd9Sstevel@tonic-gate ddi_acc_handle_t gp_handle; 687c478bd9Sstevel@tonic-gate uint8_t *gp_regs; 697c478bd9Sstevel@tonic-gate }; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate #define getsoftc(minor) \ 727c478bd9Sstevel@tonic-gate ((struct gpio_softc *)ddi_get_soft_state(statep, (minor))) 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */ 757c478bd9Sstevel@tonic-gate static int gpio_attach(dev_info_t *, ddi_attach_cmd_t); 767c478bd9Sstevel@tonic-gate static int gpio_detach(dev_info_t *, ddi_detach_cmd_t); 777c478bd9Sstevel@tonic-gate static int gpio_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 787c478bd9Sstevel@tonic-gate static int gpio_open(dev_t *, int, int, cred_t *); 797c478bd9Sstevel@tonic-gate static int gpio_close(dev_t, int, int, cred_t *); 807c478bd9Sstevel@tonic-gate static int gpio_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate struct cb_ops gpio_cb_ops = { 837c478bd9Sstevel@tonic-gate gpio_open, 847c478bd9Sstevel@tonic-gate gpio_close, 857c478bd9Sstevel@tonic-gate nodev, 867c478bd9Sstevel@tonic-gate nodev, 877c478bd9Sstevel@tonic-gate nodev, /* dump */ 887c478bd9Sstevel@tonic-gate nodev, 897c478bd9Sstevel@tonic-gate nodev, 907c478bd9Sstevel@tonic-gate gpio_ioctl, 917c478bd9Sstevel@tonic-gate nodev, /* devmap */ 927c478bd9Sstevel@tonic-gate nodev, 937c478bd9Sstevel@tonic-gate nodev, 947c478bd9Sstevel@tonic-gate nochpoll, 957c478bd9Sstevel@tonic-gate ddi_prop_op, 967c478bd9Sstevel@tonic-gate NULL, /* for STREAMS drivers */ 977c478bd9Sstevel@tonic-gate D_NEW | D_MP, /* driver compatibility flag */ 987c478bd9Sstevel@tonic-gate CB_REV, 997c478bd9Sstevel@tonic-gate nodev, 1007c478bd9Sstevel@tonic-gate nodev 1017c478bd9Sstevel@tonic-gate }; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static struct dev_ops gpio_dev_ops = { 1047c478bd9Sstevel@tonic-gate DEVO_REV, /* driver build version */ 1057c478bd9Sstevel@tonic-gate 0, /* device reference count */ 1067c478bd9Sstevel@tonic-gate gpio_getinfo, 1077c478bd9Sstevel@tonic-gate nulldev, 1087c478bd9Sstevel@tonic-gate nulldev, /* probe */ 1097c478bd9Sstevel@tonic-gate gpio_attach, 1107c478bd9Sstevel@tonic-gate gpio_detach, 1117c478bd9Sstevel@tonic-gate nulldev, /* reset */ 1127c478bd9Sstevel@tonic-gate &gpio_cb_ops, 1137c478bd9Sstevel@tonic-gate (struct bus_ops *)NULL, 114*19397407SSherry Moore nulldev, /* power */ 115*19397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 1167c478bd9Sstevel@tonic-gate }; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* module configuration stuff */ 1197c478bd9Sstevel@tonic-gate static void *statep; 1207c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1217c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1227c478bd9Sstevel@tonic-gate &mod_driverops, 123*19397407SSherry Moore "gpio driver", 1247c478bd9Sstevel@tonic-gate &gpio_dev_ops 1257c478bd9Sstevel@tonic-gate }; 1267c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1277c478bd9Sstevel@tonic-gate MODREV_1, 1287c478bd9Sstevel@tonic-gate &modldrv, 1297c478bd9Sstevel@tonic-gate 0 1307c478bd9Sstevel@tonic-gate }; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate int 1347c478bd9Sstevel@tonic-gate _init(void) 1357c478bd9Sstevel@tonic-gate { 1367c478bd9Sstevel@tonic-gate int e; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate if (e = ddi_soft_state_init(&statep, sizeof (struct gpio_softc), 1)) { 1397c478bd9Sstevel@tonic-gate return (e); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate if ((e = mod_install(&modlinkage)) != 0) { 1427c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&statep); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate return (e); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate int 1507c478bd9Sstevel@tonic-gate _fini(void) 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate int e; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate if ((e = mod_remove(&modlinkage)) != 0) { 1557c478bd9Sstevel@tonic-gate return (e); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&statep); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate int 1647c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1717c478bd9Sstevel@tonic-gate static int 1727c478bd9Sstevel@tonic-gate gpio_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 1737c478bd9Sstevel@tonic-gate { 1747c478bd9Sstevel@tonic-gate int instance = getminor((dev_t)arg); 1757c478bd9Sstevel@tonic-gate int retval = DDI_SUCCESS; 1767c478bd9Sstevel@tonic-gate struct gpio_softc *softc; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate switch (cmd) { 1797c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1807c478bd9Sstevel@tonic-gate if ((softc = getsoftc(instance)) == NULL) { 1817c478bd9Sstevel@tonic-gate *result = (void *)NULL; 1827c478bd9Sstevel@tonic-gate retval = DDI_FAILURE; 1837c478bd9Sstevel@tonic-gate } else 1847c478bd9Sstevel@tonic-gate *result = (void *)softc->gp_dip; 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 188f47a9c50Smathue *result = (void *)(uintptr_t)instance; 1897c478bd9Sstevel@tonic-gate break; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate default: 1927c478bd9Sstevel@tonic-gate retval = DDI_FAILURE; 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate return (retval); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate static int 2007c478bd9Sstevel@tonic-gate gpio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2017c478bd9Sstevel@tonic-gate { 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate int instance; 2047c478bd9Sstevel@tonic-gate struct gpio_softc *softc = NULL; 2057c478bd9Sstevel@tonic-gate ddi_device_acc_attr_t dev_attr; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate switch (cmd) { 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate case DDI_ATTACH: 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate /* Allocate and get the soft state structure for this instance. */ 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 2147c478bd9Sstevel@tonic-gate DBG(dip, "attach: instance is %d", instance, 0, 0, 0, 0); 2157c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(statep, instance) != DDI_SUCCESS) 2167c478bd9Sstevel@tonic-gate goto attach_failed; 2177c478bd9Sstevel@tonic-gate softc = getsoftc(instance); 2187c478bd9Sstevel@tonic-gate softc->gp_dip = dip; 2197c478bd9Sstevel@tonic-gate softc->gp_state = 0; 2207c478bd9Sstevel@tonic-gate mutex_init(&softc->gp_mutex, NULL, MUTEX_DRIVER, NULL); 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate /* Map in the gpio device registers. */ 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 2257c478bd9Sstevel@tonic-gate dev_attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC; 2267c478bd9Sstevel@tonic-gate dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 2277c478bd9Sstevel@tonic-gate if (ddi_regs_map_setup(dip, 0, (caddr_t *)&softc->gp_regs, 0, 0, 2287c478bd9Sstevel@tonic-gate &dev_attr, &softc->gp_handle) != DDI_SUCCESS) 2297c478bd9Sstevel@tonic-gate goto attach_failed; 230f47a9c50Smathue DBG(dip, "attach: regs=0x%p", (uintptr_t)softc->gp_regs, 231f47a9c50Smathue 0, 0, 0, 0); 2327c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 1 data is %x", 233f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[0]), 234f47a9c50Smathue 0, 0, 0, 0); 2357c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 1 direction is %x", 236f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[1]), 237f47a9c50Smathue 0, 0, 0, 0); 2387c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 1 output type is %x", 239f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[2]), 240f47a9c50Smathue 0, 0, 0, 0); 2417c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 1 pull up control type is %x", 242f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[3]), 243f47a9c50Smathue 0, 0, 0, 0); 2447c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 2 data is %x", 245f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[4]), 246f47a9c50Smathue 0, 0, 0, 0); 2477c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 2 direction is %x", 248f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[5]), 249f47a9c50Smathue 0, 0, 0, 0); 2507c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 2 output type is %x", 251f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[6]), 252f47a9c50Smathue 0, 0, 0, 0); 2537c478bd9Sstevel@tonic-gate DBG(dip, "attach: port 2 pull up control type is %x", 254f47a9c50Smathue (uintptr_t)ddi_get8(softc->gp_handle, &softc->gp_regs[7]), 255f47a9c50Smathue 0, 0, 0, 0); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* Create device minor nodes. */ 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "gpio", S_IFCHR, 2607c478bd9Sstevel@tonic-gate instance, NULL, NULL) == DDI_FAILURE) { 2617c478bd9Sstevel@tonic-gate ddi_regs_map_free(&softc->gp_handle); 2627c478bd9Sstevel@tonic-gate goto attach_failed; 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate ddi_report_dev(dip); 2667c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate case DDI_RESUME: 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate /* Nothing to do for a resume. */ 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate default: 2757c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate attach_failed: 2797c478bd9Sstevel@tonic-gate if (softc) { 2807c478bd9Sstevel@tonic-gate mutex_destroy(&softc->gp_mutex); 2817c478bd9Sstevel@tonic-gate if (softc->gp_handle) 2827c478bd9Sstevel@tonic-gate ddi_regs_map_free(&softc->gp_handle); 2837c478bd9Sstevel@tonic-gate ddi_soft_state_free(statep, instance); 2847c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate static int 2917c478bd9Sstevel@tonic-gate gpio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate int instance; 2947c478bd9Sstevel@tonic-gate struct gpio_softc *softc; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate switch (cmd) { 2977c478bd9Sstevel@tonic-gate case DDI_DETACH: 2987c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 2997c478bd9Sstevel@tonic-gate DBG(dip, "detach: instance is %d", instance, 0, 0, 0, 0); 3007c478bd9Sstevel@tonic-gate if ((softc = getsoftc(instance)) == NULL) 3017c478bd9Sstevel@tonic-gate return (ENXIO); 3027c478bd9Sstevel@tonic-gate mutex_destroy(&softc->gp_mutex); 3037c478bd9Sstevel@tonic-gate ddi_regs_map_free(&softc->gp_handle); 3047c478bd9Sstevel@tonic-gate ddi_soft_state_free(statep, instance); 3057c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 3067c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 3097c478bd9Sstevel@tonic-gate /* Nothing to do in the suspend case. */ 3107c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate default: 3137c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3197c478bd9Sstevel@tonic-gate static int 3207c478bd9Sstevel@tonic-gate gpio_open(dev_t *devp, int flag, int otyp, cred_t *credp) 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate int instance = getminor(*devp); 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate DBG(NULL, "open: instance is %d", instance, 0, 0, 0, 0); 3257c478bd9Sstevel@tonic-gate return (getsoftc(instance) == NULL ? ENXIO : 0); 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3307c478bd9Sstevel@tonic-gate static int 3317c478bd9Sstevel@tonic-gate gpio_close(dev_t dev, int flag, int otyp, cred_t *credp) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate int instance = getminor(dev); 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate DBG(NULL, "close: instance is %d", instance, 0, 0, 0, 0); 3367c478bd9Sstevel@tonic-gate return (getsoftc(instance) == NULL ? ENXIO : 0); 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3417c478bd9Sstevel@tonic-gate static int 3427c478bd9Sstevel@tonic-gate gpio_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 3437c478bd9Sstevel@tonic-gate int *rvalp) 3447c478bd9Sstevel@tonic-gate { 3457c478bd9Sstevel@tonic-gate int instance = getminor(dev); 3467c478bd9Sstevel@tonic-gate struct gpio_softc *softc = getsoftc(instance); 3477c478bd9Sstevel@tonic-gate gpio_87317_op_t info; 3487c478bd9Sstevel@tonic-gate uint8_t byte; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate DBG(softc->gp_dip, "ioctl: instance is %d", instance, 0, 0, 0, 0); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if (softc == NULL) 3537c478bd9Sstevel@tonic-gate return (ENXIO); 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate /* Copy the command from user space. */ 3567c478bd9Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&info, sizeof (gpio_87317_op_t), 3577c478bd9Sstevel@tonic-gate mode) != 0) 3587c478bd9Sstevel@tonic-gate return (EFAULT); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate /* Check the command arguments. We only support port 1 in bank 0. */ 3617c478bd9Sstevel@tonic-gate if ((info.gpio_bank != 0) || 3627c478bd9Sstevel@tonic-gate (info.gpio_offset != GPIO_87317_PORT1_DATA)) { 3637c478bd9Sstevel@tonic-gate return (EINVAL); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* Grap the instance's mutex to insure exclusive access. */ 3677c478bd9Sstevel@tonic-gate mutex_enter(&softc->gp_mutex); 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate /* Get the contents of the GPIO register we're suppose to modify. */ 3707c478bd9Sstevel@tonic-gate byte = ddi_get8(softc->gp_handle, &softc->gp_regs[info.gpio_offset]); 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate switch (cmd) { 3737c478bd9Sstevel@tonic-gate case GPIO_CMD_SET_BITS: 3747c478bd9Sstevel@tonic-gate DBG(softc->gp_dip, "ioctl: SET_BITS, byte is %x", byte, 0, 0, 3757c478bd9Sstevel@tonic-gate 0, 0); 3767c478bd9Sstevel@tonic-gate byte |= info.gpio_data; 3777c478bd9Sstevel@tonic-gate ddi_put8(softc->gp_handle, &softc->gp_regs[info.gpio_offset], 3787c478bd9Sstevel@tonic-gate byte); 3797c478bd9Sstevel@tonic-gate byte = ddi_get8(softc->gp_handle, 3807c478bd9Sstevel@tonic-gate &softc->gp_regs[info.gpio_offset]); 3817c478bd9Sstevel@tonic-gate DBG(softc->gp_dip, "ioctl: SET_BITS, byte is %x", byte, 0, 0, 3827c478bd9Sstevel@tonic-gate 0, 0); 3837c478bd9Sstevel@tonic-gate break; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate case GPIO_CMD_CLR_BITS: 3867c478bd9Sstevel@tonic-gate DBG(softc->gp_dip, "ioctl: CLR_BITS, byte is %x", byte, 0, 0, 3877c478bd9Sstevel@tonic-gate 0, 0); 3887c478bd9Sstevel@tonic-gate byte &= ~info.gpio_data; 3897c478bd9Sstevel@tonic-gate ddi_put8(softc->gp_handle, &softc->gp_regs[info.gpio_offset], 3907c478bd9Sstevel@tonic-gate byte); 3917c478bd9Sstevel@tonic-gate byte = ddi_get8(softc->gp_handle, 3927c478bd9Sstevel@tonic-gate &softc->gp_regs[info.gpio_offset]); 3937c478bd9Sstevel@tonic-gate DBG(softc->gp_dip, "ioctl: CLR_BITS, byte is %x", byte, 0, 0, 3947c478bd9Sstevel@tonic-gate 0, 0); 3957c478bd9Sstevel@tonic-gate break; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate case GPIO_CMD_GET: 3987c478bd9Sstevel@tonic-gate DBG(softc->gp_dip, "ioctl: GPIO_CMD_GET", 0, 0, 0, 0, 0); 3997c478bd9Sstevel@tonic-gate info.gpio_data = byte; 4007c478bd9Sstevel@tonic-gate if (ddi_copyout((caddr_t)&info, (caddr_t)arg, 4017c478bd9Sstevel@tonic-gate sizeof (gpio_87317_op_t), mode) != 0) { 4027c478bd9Sstevel@tonic-gate mutex_exit(&softc->gp_mutex); 4037c478bd9Sstevel@tonic-gate return (EFAULT); 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate break; 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate case GPIO_CMD_SET: 4087c478bd9Sstevel@tonic-gate DBG(softc->gp_dip, "ioctl: GPIO_CMD_SET", 0, 0, 0, 0, 0); 4097c478bd9Sstevel@tonic-gate ddi_put8(softc->gp_handle, &softc->gp_regs[info.gpio_offset], 4107c478bd9Sstevel@tonic-gate info.gpio_data); 4117c478bd9Sstevel@tonic-gate break; 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate default: 4147c478bd9Sstevel@tonic-gate mutex_exit(&softc->gp_mutex); 4157c478bd9Sstevel@tonic-gate return (EINVAL); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate mutex_exit(&softc->gp_mutex); 4197c478bd9Sstevel@tonic-gate return (0); 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate #ifdef DEBUG 4247c478bd9Sstevel@tonic-gate void 4257c478bd9Sstevel@tonic-gate gpio_debug(dev_info_t *dip, char *format, uint_t arg1, uint_t arg2, uint_t arg3, 4267c478bd9Sstevel@tonic-gate uint_t arg4, uint_t arg5) 4277c478bd9Sstevel@tonic-gate { 4287c478bd9Sstevel@tonic-gate if (gpio_debug_flag == 0) { 4297c478bd9Sstevel@tonic-gate return; 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate if (dip == NULL) { 4337c478bd9Sstevel@tonic-gate prom_printf("gpio: "); 4347c478bd9Sstevel@tonic-gate } else { 4357c478bd9Sstevel@tonic-gate prom_printf("%s%d: ", ddi_driver_name(dip), 4367c478bd9Sstevel@tonic-gate ddi_get_instance(dip)); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate prom_printf(format, arg1, arg2, arg3, arg4, arg5); 4397c478bd9Sstevel@tonic-gate prom_printf("\n"); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate #endif 442