apple_smc.c revision 1.2       1  1.1  riastrad /*	$NetBSD: apple_smc.c,v 1.2 2014/04/01 17:48:39 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Apple System Management Controller
      5  1.1  riastrad  */
      6  1.1  riastrad 
      7  1.1  riastrad /*-
      8  1.1  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      9  1.1  riastrad  * All rights reserved.
     10  1.1  riastrad  *
     11  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
     12  1.1  riastrad  * by Taylor R. Campbell.
     13  1.1  riastrad  *
     14  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     15  1.1  riastrad  * modification, are permitted provided that the following conditions
     16  1.1  riastrad  * are met:
     17  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     18  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     19  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     20  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     21  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     22  1.1  riastrad  *
     23  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  riastrad  */
     35  1.1  riastrad 
     36  1.1  riastrad #include <sys/cdefs.h>
     37  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: apple_smc.c,v 1.2 2014/04/01 17:48:39 riastradh Exp $");
     38  1.1  riastrad 
     39  1.1  riastrad #include <sys/types.h>
     40  1.1  riastrad #include <sys/param.h>
     41  1.1  riastrad #include <sys/device.h>
     42  1.1  riastrad #include <sys/errno.h>
     43  1.1  riastrad #include <sys/kmem.h>
     44  1.2  riastrad #include <sys/module.h>
     45  1.1  riastrad #include <sys/mutex.h>
     46  1.2  riastrad #include <sys/rbtree.h>
     47  1.2  riastrad #include <sys/rwlock.h>
     48  1.1  riastrad #if 0                           /* XXX sysctl */
     49  1.1  riastrad #include <sys/sysctl.h>
     50  1.1  riastrad #endif
     51  1.1  riastrad #include <sys/systm.h>
     52  1.1  riastrad 
     53  1.1  riastrad #include <dev/ic/apple_smc.h>
     54  1.1  riastrad #include <dev/ic/apple_smcreg.h>
     55  1.1  riastrad #include <dev/ic/apple_smcvar.h>
     56  1.1  riastrad 
     57  1.2  riastrad #define	APPLE_SMC_BUS	"applesmcbus"
     58  1.2  riastrad 
     59  1.2  riastrad static int	apple_smc_dev_compare_nodes(void *, const void *,
     60  1.2  riastrad 		    const void *);
     61  1.2  riastrad static int	apple_smc_dev_compare_key(void *, const void *, const void *);
     62  1.2  riastrad static int	apple_smc_init(void);
     63  1.2  riastrad static int	apple_smc_fini(void);
     64  1.1  riastrad static uint8_t	apple_smc_bus_read_1(struct apple_smc_tag *, bus_size_t);
     65  1.1  riastrad static void	apple_smc_bus_write_1(struct apple_smc_tag *, bus_size_t,
     66  1.1  riastrad 		    uint8_t);
     67  1.1  riastrad static int	apple_smc_read_data(struct apple_smc_tag *, uint8_t *);
     68  1.1  riastrad static int	apple_smc_write(struct apple_smc_tag *, bus_size_t, uint8_t);
     69  1.1  riastrad static int	apple_smc_write_cmd(struct apple_smc_tag *, uint8_t);
     70  1.1  riastrad static int	apple_smc_write_data(struct apple_smc_tag *, uint8_t);
     71  1.1  riastrad static int	apple_smc_begin(struct apple_smc_tag *, uint8_t,
     72  1.1  riastrad 		    const char *, uint8_t);
     73  1.1  riastrad static int	apple_smc_input(struct apple_smc_tag *, uint8_t,
     74  1.1  riastrad 		    const char *, void *, uint8_t);
     75  1.1  riastrad static int	apple_smc_output(struct apple_smc_tag *, uint8_t,
     76  1.1  riastrad 		    const char *, const void *, uint8_t);
     77  1.2  riastrad 
     78  1.2  riastrad struct apple_smc_dev {
     80  1.2  riastrad 	char		asd_name[APPLE_SMC_DEVICE_NAME_SIZE];
     81  1.2  riastrad 	rb_node_t	asd_node;
     82  1.2  riastrad 	device_t	asd_dev[];
     83  1.2  riastrad };
     84  1.2  riastrad 
     85  1.2  riastrad static int
     86  1.2  riastrad apple_smc_dev_compare_nodes(void *context __unused, const void *va,
     87  1.2  riastrad     const void *vb)
     88  1.2  riastrad {
     89  1.2  riastrad 	const struct apple_smc_dev *const a = va;
     90  1.2  riastrad 	const struct apple_smc_dev *const b = vb;
     91  1.2  riastrad 
     92  1.2  riastrad 	return strncmp(a->asd_name, b->asd_name, APPLE_SMC_DEVICE_NAME_SIZE);
     93  1.2  riastrad }
     94  1.2  riastrad 
     95  1.2  riastrad static int
     96  1.2  riastrad apple_smc_dev_compare_key(void *context __unused, const void *vn,
     97  1.2  riastrad     const void *vk)
     98  1.2  riastrad {
     99  1.2  riastrad 	const struct apple_smc_dev *const dev = vn;
    100  1.2  riastrad 	const char *const key = vk;
    101  1.2  riastrad 
    102  1.2  riastrad 	return strncmp(dev->asd_name, key, APPLE_SMC_DEVICE_NAME_SIZE);
    103  1.1  riastrad }
    104  1.2  riastrad 
    105  1.2  riastrad static krwlock_t apple_smc_registered_devices_lock;
    106  1.2  riastrad static rb_tree_t apple_smc_registered_devices;
    107  1.2  riastrad static unsigned int apple_smc_n_registered_devices;
    108  1.2  riastrad 
    109  1.2  riastrad static const rb_tree_ops_t apple_smc_dev_tree_ops = {
    110  1.2  riastrad 	.rbto_compare_nodes	= &apple_smc_dev_compare_nodes,
    111  1.2  riastrad 	.rbto_compare_key	= &apple_smc_dev_compare_key,
    112  1.2  riastrad 	.rbto_node_offset	= offsetof(struct apple_smc_dev, asd_node),
    113  1.2  riastrad };
    114  1.2  riastrad 
    115  1.2  riastrad static int
    116  1.2  riastrad apple_smc_init(void)
    117  1.2  riastrad {
    118  1.2  riastrad 
    119  1.2  riastrad 	rw_init(&apple_smc_registered_devices_lock);
    120  1.2  riastrad 	rb_tree_init(&apple_smc_registered_devices, &apple_smc_dev_tree_ops);
    121  1.2  riastrad 	apple_smc_n_registered_devices = 0;
    122  1.2  riastrad 
    123  1.2  riastrad 	/* Success!  */
    124  1.2  riastrad 	return 0;
    125  1.2  riastrad }
    126  1.2  riastrad 
    127  1.2  riastrad static int
    128  1.1  riastrad apple_smc_fini(void)
    129  1.1  riastrad {
    130  1.2  riastrad 
    131  1.2  riastrad 	/* Refuse to unload if there remain any registered devices.  */
    132  1.2  riastrad 	if (apple_smc_n_registered_devices)
    133  1.2  riastrad 		return EBUSY;
    134  1.2  riastrad 
    135  1.2  riastrad 	/* The tree should be empty in this case.  */
    136  1.2  riastrad 	KASSERT(rb_tree_iterate(&apple_smc_registered_devices, NULL,
    137  1.1  riastrad 		RB_DIR_RIGHT) == NULL);
    138  1.2  riastrad 
    139  1.2  riastrad #if 0				/* XXX no rb_tree_destroy */
    140  1.1  riastrad 	rb_tree_destroy(&apple_smc_registered_devices);
    141  1.2  riastrad #endif
    142  1.1  riastrad 	rw_destroy(&apple_smc_registered_devices_lock);
    143  1.2  riastrad 
    144  1.2  riastrad 	/* Success!  */
    145  1.1  riastrad 	return 0;
    146  1.1  riastrad }
    147  1.1  riastrad 
    148  1.2  riastrad int
    149  1.1  riastrad apple_smc_register_device(const char name[APPLE_SMC_DEVICE_NAME_SIZE])
    150  1.2  riastrad {
    151  1.2  riastrad 	int error;
    152  1.2  riastrad 
    153  1.2  riastrad 	/* Paranoia about null termination.  */
    154  1.1  riastrad 	KASSERT(name[strnlen(name, (sizeof(name) - 1))] == '\0');
    155  1.2  riastrad 
    156  1.2  riastrad 	/* Make a new record for the registration.  */
    157  1.2  riastrad 	struct apple_smc_dev *const dev =
    158  1.2  riastrad 	    kmem_alloc(offsetof(struct apple_smc_dev, asd_dev[0]), KM_SLEEP);
    159  1.2  riastrad 	(void)strlcpy(dev->asd_name, name, sizeof(dev->asd_name));
    160  1.2  riastrad 
    161  1.2  riastrad 	rw_enter(&apple_smc_registered_devices_lock, RW_WRITER);
    162  1.2  riastrad 
    163  1.2  riastrad 	/* Fail if there are too many.  We really oughtn't get here.  */
    164  1.2  riastrad 	if (apple_smc_n_registered_devices == UINT_MAX) {
    165  1.2  riastrad 		error = ENOMEM;
    166  1.2  riastrad 		goto out;
    167  1.1  riastrad 	}
    168  1.2  riastrad 
    169  1.2  riastrad 	/* Fail if the name is already registered.  */
    170  1.2  riastrad 	struct apple_smc_dev *const collision =
    171  1.2  riastrad 	    rb_tree_insert_node(&apple_smc_registered_devices, dev);
    172  1.2  riastrad 	if (collision != dev) {
    173  1.2  riastrad 		kmem_free(dev, offsetof(struct apple_smc_dev, asd_dev[0]));
    174  1.2  riastrad 		error = EEXIST;
    175  1.2  riastrad 		goto out;
    176  1.1  riastrad 	}
    177  1.2  riastrad 
    178  1.2  riastrad 	/* Success!  */
    179  1.2  riastrad 	apple_smc_n_registered_devices++;
    180  1.1  riastrad 	error = 0;
    181  1.2  riastrad 
    182  1.2  riastrad out:
    183  1.2  riastrad 	rw_exit(&apple_smc_registered_devices_lock);
    184  1.1  riastrad 	return error;
    185  1.1  riastrad }
    186  1.2  riastrad 
    187  1.2  riastrad void
    188  1.2  riastrad apple_smc_unregister_device(const char name[APPLE_SMC_DEVICE_NAME_SIZE])
    189  1.2  riastrad {
    190  1.2  riastrad 
    191  1.2  riastrad 	KASSERT(name[strnlen(name, (sizeof(name) - 1))] == '\0');
    192  1.2  riastrad 
    193  1.2  riastrad 	rw_enter(&apple_smc_registered_devices_lock, RW_WRITER);
    194  1.2  riastrad 
    195  1.2  riastrad 	/* Find the node.  */
    196  1.2  riastrad 	struct apple_smc_dev *const dev =
    197  1.2  riastrad 	    rb_tree_find_node(&apple_smc_registered_devices, name);
    198  1.2  riastrad 	if (dev == NULL) {
    199  1.2  riastrad 		printf("%s: device was never registered: %s\n", __func__,
    200  1.2  riastrad 		    name);
    201  1.2  riastrad 		goto out;
    202  1.2  riastrad 	}
    203  1.2  riastrad 
    204  1.2  riastrad 	/* If we found one, this ought to be at least 1.  */
    205  1.2  riastrad 	KASSERT(apple_smc_n_registered_devices > 0);
    206  1.2  riastrad 
    207  1.2  riastrad 	/* Remove it, but wait until unlocked to free it (paranoia).  */
    208  1.2  riastrad 	rb_tree_remove_node(&apple_smc_registered_devices, dev);
    209  1.2  riastrad 	apple_smc_n_registered_devices--;
    210  1.2  riastrad 
    211  1.2  riastrad out:
    212  1.2  riastrad 	rw_exit(&apple_smc_registered_devices_lock);
    213  1.2  riastrad 	if (dev != NULL)
    214  1.2  riastrad 		kmem_free(dev, offsetof(struct apple_smc_dev, asd_dev[0]));
    215  1.2  riastrad }
    216  1.2  riastrad 
    217  1.2  riastrad void
    219  1.1  riastrad apple_smc_attach(struct apple_smc_tag *smc)
    220  1.2  riastrad {
    221  1.2  riastrad 
    222  1.2  riastrad 	mutex_init(&smc->smc_lock, MUTEX_DEFAULT, IPL_NONE);
    223  1.2  riastrad 	rb_tree_init(&smc->smc_devices, &apple_smc_dev_tree_ops);
    224  1.2  riastrad 
    225  1.2  riastrad #if 0				/* XXX sysctl */
    226  1.2  riastrad 	apple_smc_sysctl_setup(smc);
    227  1.2  riastrad #endif
    228  1.1  riastrad 
    229  1.1  riastrad         (void)apple_smc_rescan(smc, NULL, NULL);
    230  1.1  riastrad }
    231  1.1  riastrad 
    232  1.1  riastrad int
    233  1.1  riastrad apple_smc_detach(struct apple_smc_tag *smc, int flags)
    234  1.1  riastrad {
    235  1.1  riastrad 	int error;
    236  1.1  riastrad 
    237  1.1  riastrad 	error = config_detach_children(smc->smc_dev, flags);
    238  1.1  riastrad 	if (error)
    239  1.1  riastrad 		return error;
    240  1.1  riastrad 
    241  1.1  riastrad #if 0				/* XXX sysctl */
    242  1.1  riastrad 	sysctl_teardown(&smc->smc_log);
    243  1.1  riastrad #endif
    244  1.1  riastrad 
    245  1.1  riastrad 	return 0;
    246  1.2  riastrad }
    247  1.2  riastrad 
    248  1.2  riastrad int
    249  1.2  riastrad apple_smc_rescan(struct apple_smc_tag *smc, const char *ifattr,
    250  1.2  riastrad     const int *locators __unused)
    251  1.2  riastrad {
    252  1.2  riastrad 	struct apple_smc_attach_args asa;
    253  1.2  riastrad 	struct apple_smc_dev *registered, *attached;
    254  1.2  riastrad 	device_t child;
    255  1.2  riastrad 
    256  1.2  riastrad 	if (!ifattr_match(ifattr, APPLE_SMC_BUS))
    257  1.2  riastrad 		return 0;
    258  1.2  riastrad 
    259  1.2  riastrad 	(void)memset(&asa, 0, sizeof asa);
    260  1.2  riastrad 	asa.asa_smc = smc;
    261  1.2  riastrad #if 0				/* XXX sysctl */
    262  1.2  riastrad 	asa.asa_sysctlnode = smc->smc_sysctlnode;
    263  1.2  riastrad #endif
    264  1.2  riastrad 
    265  1.2  riastrad 	/*
    266  1.2  riastrad 	 * Go through all the registered SMC devices and try to attach
    267  1.2  riastrad 	 * them.
    268  1.2  riastrad 	 *
    269  1.2  riastrad 	 * XXX Horrible quadratic-time loop to avoid holding the rwlock
    270  1.2  riastrad 	 * during allocation.  Fortunately, there are not likely to be
    271  1.2  riastrad 	 * many of these devices.
    272  1.2  riastrad 	 */
    273  1.2  riastrad restart:
    274  1.2  riastrad 	registered = NULL;
    275  1.2  riastrad 	rw_enter(&apple_smc_registered_devices_lock, RW_READER);
    276  1.2  riastrad 	while ((registered = rb_tree_iterate(&apple_smc_registered_devices,
    277  1.2  riastrad 		    registered, RB_DIR_RIGHT)) != NULL) {
    278  1.2  riastrad 		char name[APPLE_SMC_DEVICE_NAME_SIZE];
    279  1.2  riastrad 		CTASSERT(sizeof(name) == sizeof(registered->asd_name));
    280  1.2  riastrad 
    281  1.2  riastrad 		/* Paranoia about null termination.  */
    282  1.2  riastrad 		KASSERT(registered->asd_name[strnlen(registered->asd_name,
    283  1.2  riastrad 				(sizeof(registered->asd_name) - 1))] == '\0');
    284  1.2  riastrad 
    285  1.2  riastrad 		/* Skip it if we already have it attached.  */
    286  1.2  riastrad 		attached = rb_tree_find_node(&smc->smc_devices,
    287  1.2  riastrad 		    registered->asd_name);
    288  1.2  riastrad 		if (attached != NULL)
    289  1.2  riastrad 			continue;
    290  1.2  riastrad 
    291  1.2  riastrad 		/* Try to match it autoconfily.  */
    292  1.2  riastrad 		asa.asa_device = registered->asd_name;
    293  1.2  riastrad 		child = config_found_ia(smc->smc_dev, APPLE_SMC_BUS, &asa,
    294  1.2  riastrad 		    NULL);
    295  1.2  riastrad 		if (child == NULL)
    296  1.2  riastrad 			continue;
    297  1.2  riastrad 
    298  1.2  riastrad 		/* Save the name while we drop the lock.  */
    299  1.2  riastrad 		(void)strlcpy(name, registered->asd_name, sizeof(name));
    300  1.2  riastrad 
    301  1.2  riastrad 		/* Drop the lock so we can allocate.  */
    302  1.2  riastrad 		rw_exit(&apple_smc_registered_devices_lock);
    303  1.2  riastrad 
    304  1.2  riastrad 		/* Create a new record for the attachment.  */
    305  1.2  riastrad 		attached = kmem_alloc(offsetof(struct apple_smc_dev,
    306  1.2  riastrad 			asd_dev[1]), KM_SLEEP);
    307  1.2  riastrad 		(void)strlcpy(attached->asd_name, name, sizeof(name));
    308  1.2  riastrad 		attached->asd_dev[0] = child;
    309  1.2  riastrad 
    310  1.2  riastrad 		/* Store it in the tree.  */
    311  1.2  riastrad 		struct apple_smc_dev *const collision __unused =
    312  1.2  riastrad 		    rb_tree_insert_node(&smc->smc_devices, attached);
    313  1.2  riastrad 		KASSERT(collision == attached);
    314  1.2  riastrad 
    315  1.2  riastrad 		/* Start back at the beginning since we dropped the lock.  */
    316  1.2  riastrad 		goto restart;
    317  1.2  riastrad 	}
    318  1.2  riastrad 	rw_exit(&apple_smc_registered_devices_lock);
    319  1.2  riastrad 
    320  1.2  riastrad 	/* Success!  */
    321  1.2  riastrad         return 0;
    322  1.2  riastrad }
    323  1.2  riastrad 
    324  1.2  riastrad void
    325  1.2  riastrad apple_smc_child_detached(struct apple_smc_tag *smc, device_t child)
    326  1.2  riastrad {
    327  1.2  riastrad 	struct apple_smc_dev *dev, *next;
    328  1.2  riastrad 	bool done = false;
    329  1.2  riastrad 
    330  1.2  riastrad 	/*
    331  1.2  riastrad 	 * Go through the list of attached devices safely with respect
    332  1.2  riastrad 	 * to removal and remove any matching entries.  There should be
    333  1.2  riastrad 	 * only one, but paranoia.
    334  1.2  riastrad 	 */
    335  1.2  riastrad 	for (dev = rb_tree_iterate(&smc->smc_devices, NULL, RB_DIR_RIGHT);
    336  1.2  riastrad 	     (dev != NULL?
    337  1.2  riastrad 		 (next = rb_tree_iterate(&smc->smc_devices, dev,
    338  1.2  riastrad 		     RB_DIR_RIGHT), true)
    339  1.2  riastrad 		 : false);
    340  1.2  riastrad 	     dev = next) {
    341  1.2  riastrad 		if (dev->asd_dev[0] != child)
    342  1.2  riastrad 			continue;
    343  1.2  riastrad 		if (done)
    344  1.2  riastrad 			printf("apple smc child doubly attached: %s\n",
    345  1.2  riastrad 			    dev->asd_name);
    346  1.2  riastrad 		rb_tree_remove_node(&smc->smc_devices, dev);
    347  1.2  riastrad 		kmem_free(dev, offsetof(struct apple_smc_dev, asd_dev[1]));
    348  1.2  riastrad 	}
    349  1.1  riastrad }
    350  1.1  riastrad 
    351  1.1  riastrad static uint8_t
    353  1.1  riastrad apple_smc_bus_read_1(struct apple_smc_tag *smc, bus_size_t reg)
    354  1.1  riastrad {
    355  1.1  riastrad 
    356  1.1  riastrad 	return bus_space_read_1(smc->smc_bst, smc->smc_bsh, reg);
    357  1.1  riastrad }
    358  1.1  riastrad 
    359  1.1  riastrad static void
    360  1.1  riastrad apple_smc_bus_write_1(struct apple_smc_tag *smc, bus_size_t reg, uint8_t v)
    361  1.1  riastrad {
    362  1.1  riastrad 
    363  1.1  riastrad 	bus_space_write_1(smc->smc_bst, smc->smc_bsh, reg, v);
    364  1.1  riastrad }
    365  1.1  riastrad 
    366  1.1  riastrad /*
    367  1.1  riastrad  * XXX These delays are pretty randomly chosen.  Wait in 100 us
    368  1.1  riastrad  * increments, up to a total of 1 ms.
    369  1.1  riastrad  */
    370  1.1  riastrad 
    371  1.1  riastrad static int
    372  1.1  riastrad apple_smc_read_data(struct apple_smc_tag *smc, uint8_t *byte)
    373  1.1  riastrad {
    374  1.1  riastrad 	uint8_t status;
    375  1.1  riastrad 	unsigned int i;
    376  1.1  riastrad 
    377  1.1  riastrad 	KASSERT(mutex_owned(&smc->smc_lock));
    378  1.1  riastrad 
    379  1.1  riastrad 	for (i = 0; i < 100; i++) {
    380  1.1  riastrad 		status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
    381  1.1  riastrad 		if (status & APPLE_SMC_STATUS_READ_READY) {
    382  1.1  riastrad 			*byte = apple_smc_bus_read_1(smc, APPLE_SMC_DATA);
    383  1.1  riastrad 			return 0;
    384  1.1  riastrad 		}
    385  1.1  riastrad 		DELAY(100);
    386  1.1  riastrad 	}
    387  1.1  riastrad 
    388  1.1  riastrad 	return ETIMEDOUT;
    389  1.1  riastrad }
    390  1.1  riastrad 
    391  1.1  riastrad static int
    392  1.1  riastrad apple_smc_write(struct apple_smc_tag *smc, bus_size_t reg, uint8_t byte)
    393  1.1  riastrad {
    394  1.1  riastrad 	uint8_t status;
    395  1.1  riastrad 	unsigned int i;
    396  1.1  riastrad 
    397  1.1  riastrad 	KASSERT(mutex_owned(&smc->smc_lock));
    398  1.1  riastrad 
    399  1.1  riastrad 	apple_smc_bus_write_1(smc, reg, byte);
    400  1.1  riastrad 	for (i = 0; i < 100; i++) {
    401  1.1  riastrad 		status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
    402  1.1  riastrad 		if (status & APPLE_SMC_STATUS_WRITE_ACCEPTED)
    403  1.1  riastrad 			return 0;
    404  1.1  riastrad 		DELAY(100);
    405  1.1  riastrad 		if (!(status & APPLE_SMC_STATUS_WRITE_PENDING))
    406  1.1  riastrad 			apple_smc_bus_write_1(smc, reg, byte);
    407  1.1  riastrad 	}
    408  1.1  riastrad 
    409  1.1  riastrad 	return ETIMEDOUT;
    410  1.1  riastrad }
    411  1.1  riastrad 
    412  1.1  riastrad static int
    413  1.1  riastrad apple_smc_write_cmd(struct apple_smc_tag *smc, uint8_t cmd)
    414  1.1  riastrad {
    415  1.1  riastrad 
    416  1.1  riastrad 	return apple_smc_write(smc, APPLE_SMC_CSR, cmd);
    417  1.1  riastrad }
    418  1.1  riastrad 
    419  1.1  riastrad static int
    420  1.1  riastrad apple_smc_write_data(struct apple_smc_tag *smc, uint8_t data)
    421  1.1  riastrad {
    422  1.2  riastrad 
    423  1.1  riastrad 	return apple_smc_write(smc, APPLE_SMC_DATA, data);
    424  1.1  riastrad }
    425  1.1  riastrad 
    426  1.1  riastrad static int
    428  1.1  riastrad apple_smc_begin(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
    429  1.1  riastrad     uint8_t size)
    430  1.1  riastrad {
    431  1.1  riastrad 	unsigned int i;
    432  1.1  riastrad 	int error;
    433  1.1  riastrad 
    434  1.1  riastrad 	KASSERT(mutex_owned(&smc->smc_lock));
    435  1.1  riastrad 
    436  1.1  riastrad 	error = apple_smc_write_cmd(smc, cmd);
    437  1.1  riastrad 	if (error)
    438  1.1  riastrad 		return error;
    439  1.1  riastrad 
    440  1.1  riastrad 	for (i = 0; i < 4; i++) {
    441  1.1  riastrad 		error = apple_smc_write_data(smc, key[i]);
    442  1.1  riastrad 		if (error)
    443  1.1  riastrad 			return error;
    444  1.1  riastrad 	}
    445  1.1  riastrad 
    446  1.1  riastrad 	error = apple_smc_write_data(smc, size);
    447  1.1  riastrad 	if (error)
    448  1.1  riastrad 		return error;
    449  1.1  riastrad 
    450  1.1  riastrad 	return 0;
    451  1.1  riastrad }
    452  1.1  riastrad 
    453  1.1  riastrad static int
    454  1.1  riastrad apple_smc_input(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
    455  1.1  riastrad     void *buffer, uint8_t size)
    456  1.1  riastrad {
    457  1.1  riastrad 	uint8_t *bytes = buffer;
    458  1.1  riastrad 	uint8_t i;
    459  1.1  riastrad 	int error;
    460  1.1  riastrad 
    461  1.1  riastrad 	mutex_enter(&smc->smc_lock);
    462  1.1  riastrad 	error = apple_smc_begin(smc, cmd, key, size);
    463  1.1  riastrad 	if (error)
    464  1.1  riastrad 		goto out;
    465  1.1  riastrad 
    466  1.1  riastrad 	for (i = 0; i < size; i++) {
    467  1.1  riastrad 		error = apple_smc_read_data(smc, &bytes[i]);
    468  1.1  riastrad 		if (error)
    469  1.1  riastrad 			goto out;
    470  1.1  riastrad 	}
    471  1.1  riastrad 
    472  1.1  riastrad 	/* Success!  */
    473  1.1  riastrad 	error = 0;
    474  1.1  riastrad 
    475  1.1  riastrad out:
    476  1.1  riastrad 	mutex_exit(&smc->smc_lock);
    477  1.1  riastrad 	return error;
    478  1.1  riastrad }
    479  1.1  riastrad 
    480  1.1  riastrad static int
    481  1.1  riastrad apple_smc_output(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
    482  1.1  riastrad     const void *buffer, uint8_t size)
    483  1.1  riastrad {
    484  1.1  riastrad 	const uint8_t *bytes = buffer;
    485  1.1  riastrad 	uint8_t i;
    486  1.1  riastrad 	int error;
    487  1.1  riastrad 
    488  1.1  riastrad 	mutex_enter(&smc->smc_lock);
    489  1.1  riastrad 	error = apple_smc_begin(smc, cmd, key, size);
    490  1.1  riastrad 	if (error)
    491  1.1  riastrad 		goto out;
    492  1.1  riastrad 
    493  1.1  riastrad 	for (i = 0; i < size; i++) {
    494  1.1  riastrad 		error = apple_smc_write_data(smc, bytes[i]);
    495  1.1  riastrad 		if (error)
    496  1.1  riastrad 			goto out;
    497  1.1  riastrad 	}
    498  1.1  riastrad 
    499  1.2  riastrad out:
    500  1.2  riastrad 	mutex_exit(&smc->smc_lock);
    501  1.2  riastrad 	return error;
    502  1.2  riastrad }
    503  1.2  riastrad 
    504  1.2  riastrad struct apple_smc_key {
    506  1.2  riastrad 	char ask_name[4 + 1];
    507  1.1  riastrad 	struct apple_smc_desc ask_desc;
    508  1.1  riastrad #ifdef DIAGNOSTIC
    509  1.1  riastrad 	struct apple_smc_tag *ask_smc;
    510  1.1  riastrad #endif
    511  1.1  riastrad };
    512  1.1  riastrad 
    513  1.1  riastrad const char *
    514  1.1  riastrad apple_smc_key_name(const struct apple_smc_key *key)
    515  1.1  riastrad {
    516  1.1  riastrad 
    517  1.1  riastrad 	return key->ask_name;
    518  1.1  riastrad }
    519  1.1  riastrad 
    520  1.1  riastrad const struct apple_smc_desc *
    521  1.1  riastrad apple_smc_key_desc(const struct apple_smc_key *key)
    522  1.1  riastrad {
    523  1.1  riastrad 
    524  1.1  riastrad 	return &key->ask_desc;
    525  1.1  riastrad }
    526  1.1  riastrad 
    527  1.1  riastrad uint32_t
    528  1.1  riastrad apple_smc_nkeys(struct apple_smc_tag *smc)
    529  1.1  riastrad {
    530  1.1  riastrad 
    531  1.1  riastrad 	return smc->smc_nkeys;
    532  1.1  riastrad }
    533  1.1  riastrad 
    534  1.1  riastrad int
    535  1.1  riastrad apple_smc_nth_key(struct apple_smc_tag *smc, uint32_t index,
    536  1.1  riastrad     const char type[4 + 1], struct apple_smc_key **keyp)
    537  1.1  riastrad {
    538  1.1  riastrad 	union { uint32_t u32; char name[4]; } index_be;
    539  1.1  riastrad 	struct apple_smc_key *key;
    540  1.1  riastrad 	int error;
    541  1.1  riastrad 
    542  1.1  riastrad 	if ((type != NULL) && (strlen(type) != 4))
    543  1.1  riastrad 		return EINVAL;
    544  1.1  riastrad 
    545  1.1  riastrad 	key = kmem_alloc(sizeof(*key), KM_SLEEP);
    546  1.1  riastrad #ifdef DIAGNOSTIC
    547  1.1  riastrad 	key->ask_smc = smc;
    548  1.1  riastrad #endif
    549  1.1  riastrad 
    550  1.1  riastrad 	index_be.u32 = htobe32(index);
    551  1.1  riastrad 	error = apple_smc_input(smc, APPLE_SMC_CMD_NTH_KEY, index_be.name,
    552  1.1  riastrad 	    key->ask_name, 4);
    553  1.1  riastrad 	if (error)
    554  1.1  riastrad 		goto fail;
    555  1.1  riastrad 	key->ask_name[4] = '\0';
    556  1.1  riastrad 
    557  1.1  riastrad 	CTASSERT(sizeof(key->ask_desc) == 6);
    558  1.1  riastrad 	error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
    559  1.1  riastrad 	    &key->ask_desc, 6);
    560  1.1  riastrad 	if (error)
    561  1.1  riastrad 		goto fail;
    562  1.1  riastrad 
    563  1.1  riastrad 	if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
    564  1.1  riastrad 		error = EINVAL;
    565  1.1  riastrad 		goto fail;
    566  1.1  riastrad 	}
    567  1.1  riastrad 
    568  1.1  riastrad 	/* Success!  */
    569  1.1  riastrad 	*keyp = key;
    570  1.1  riastrad 	return 0;
    571  1.1  riastrad 
    572  1.1  riastrad fail:
    573  1.1  riastrad 	kmem_free(key, sizeof(*key));
    574  1.1  riastrad 	return error;
    575  1.1  riastrad }
    576  1.1  riastrad 
    577  1.1  riastrad int
    578  1.1  riastrad apple_smc_named_key(struct apple_smc_tag *smc, const char name[4 + 1],
    579  1.1  riastrad     const char type[4 + 1], struct apple_smc_key **keyp)
    580  1.1  riastrad {
    581  1.1  riastrad 	struct apple_smc_key *key;
    582  1.1  riastrad 	int error;
    583  1.1  riastrad 
    584  1.1  riastrad 	KASSERT(name != NULL);
    585  1.1  riastrad 	if (strlen(name) != 4)
    586  1.1  riastrad 		return EINVAL;
    587  1.1  riastrad 
    588  1.1  riastrad 	if ((type != NULL) && (strlen(type) != 4))
    589  1.1  riastrad 		return EINVAL;
    590  1.1  riastrad 
    591  1.1  riastrad 	key = kmem_alloc(sizeof(*key), KM_SLEEP);
    592  1.1  riastrad #ifdef DIAGNOSTIC
    593  1.1  riastrad 	key->ask_smc = smc;
    594  1.1  riastrad #endif
    595  1.1  riastrad 	(void)memcpy(key->ask_name, name, 4);
    596  1.1  riastrad 	key->ask_name[4] = '\0';
    597  1.1  riastrad 
    598  1.1  riastrad 	CTASSERT(sizeof(key->ask_desc) == 6);
    599  1.1  riastrad 	error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
    600  1.1  riastrad 	    &key->ask_desc, 6);
    601  1.1  riastrad 	if (error)
    602  1.1  riastrad 		goto fail;
    603  1.1  riastrad 
    604  1.1  riastrad 	if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
    605  1.1  riastrad 		error = EINVAL;
    606  1.1  riastrad 		goto fail;
    607  1.1  riastrad 	}
    608  1.1  riastrad 
    609  1.1  riastrad 	*keyp = key;
    610  1.1  riastrad 	return 0;
    611  1.1  riastrad 
    612  1.1  riastrad fail:
    613  1.1  riastrad 	kmem_free(key, sizeof(*key));
    614  1.1  riastrad 	return error;
    615  1.1  riastrad }
    616  1.1  riastrad 
    617  1.1  riastrad void
    618  1.1  riastrad apple_smc_release_key(struct apple_smc_tag *smc, struct apple_smc_key *key)
    619  1.1  riastrad {
    620  1.1  riastrad 
    621  1.1  riastrad #ifdef DIAGNOSTIC
    622  1.1  riastrad 	if (key->ask_smc != smc)
    623  1.1  riastrad 		aprint_error_dev(smc->smc_dev,
    624  1.1  riastrad 		    "releasing key with wrong tag: %p != %p",
    625  1.1  riastrad 		    smc, key->ask_smc);
    626  1.1  riastrad #endif
    627  1.1  riastrad 	kmem_free(key, sizeof(*key));
    628  1.1  riastrad }
    629  1.1  riastrad 
    630  1.1  riastrad int
    631  1.1  riastrad apple_smc_key_search(struct apple_smc_tag *smc, const char *name,
    632  1.1  riastrad     uint32_t *result)
    633  1.1  riastrad {
    634  1.1  riastrad 	struct apple_smc_key *key;
    635  1.1  riastrad 	uint32_t start = 0, end = apple_smc_nkeys(smc), median;
    636  1.1  riastrad 	int error;
    637  1.1  riastrad 
    638  1.1  riastrad 	while (start < end) {
    639  1.1  riastrad 		median = (start + ((end - start) / 2));
    640  1.1  riastrad 		error = apple_smc_nth_key(smc, median, NULL, &key);
    641  1.1  riastrad 		if (error)
    642  1.1  riastrad 			return error;
    643  1.1  riastrad 
    644  1.1  riastrad 		if (memcmp(name, apple_smc_key_name(key), 4) < 0)
    645  1.1  riastrad 			end = median;
    646  1.1  riastrad 		else
    647  1.1  riastrad 			start = (median + 1);
    648  1.1  riastrad 		apple_smc_release_key(smc, key);
    649  1.2  riastrad 	}
    650  1.1  riastrad 
    651  1.1  riastrad 	*result = start;
    652  1.1  riastrad 	return 0;
    653  1.1  riastrad }
    654  1.1  riastrad 
    655  1.1  riastrad int
    657  1.1  riastrad apple_smc_read_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
    658  1.1  riastrad     void *buffer, uint8_t size)
    659  1.1  riastrad {
    660  1.1  riastrad 
    661  1.1  riastrad 	if (key->ask_desc.asd_size != size)
    662  1.1  riastrad 		return EINVAL;
    663  1.1  riastrad 	if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_READ))
    664  1.1  riastrad 		return EACCES;
    665  1.1  riastrad 
    666  1.1  riastrad 	return apple_smc_input(smc, APPLE_SMC_CMD_READ_KEY, key->ask_name,
    667  1.1  riastrad 	    buffer, size);
    668  1.1  riastrad }
    669  1.1  riastrad 
    670  1.1  riastrad int
    671  1.1  riastrad apple_smc_read_key_1(struct apple_smc_tag *smc,
    672  1.1  riastrad     const struct apple_smc_key *key, uint8_t *p)
    673  1.1  riastrad {
    674  1.1  riastrad 
    675  1.1  riastrad 	return apple_smc_read_key(smc, key, p, 1);
    676  1.1  riastrad }
    677  1.1  riastrad 
    678  1.1  riastrad int
    679  1.1  riastrad apple_smc_read_key_2(struct apple_smc_tag *smc,
    680  1.1  riastrad     const struct apple_smc_key *key, uint16_t *p)
    681  1.1  riastrad {
    682  1.1  riastrad 	uint16_t be;
    683  1.1  riastrad 	int error;
    684  1.1  riastrad 
    685  1.1  riastrad 	error = apple_smc_read_key(smc, key, &be, 2);
    686  1.1  riastrad 	if (error)
    687  1.1  riastrad 		return error;
    688  1.1  riastrad 
    689  1.1  riastrad 	*p = be16toh(be);
    690  1.1  riastrad 	return 0;
    691  1.1  riastrad }
    692  1.1  riastrad 
    693  1.1  riastrad int
    694  1.1  riastrad apple_smc_read_key_4(struct apple_smc_tag *smc,
    695  1.1  riastrad     const struct apple_smc_key *key, uint32_t *p)
    696  1.1  riastrad {
    697  1.1  riastrad 	uint32_t be;
    698  1.1  riastrad 	int error;
    699  1.1  riastrad 
    700  1.1  riastrad 	error = apple_smc_read_key(smc, key, &be, 4);
    701  1.1  riastrad 	if (error)
    702  1.1  riastrad 		return error;
    703  1.1  riastrad 
    704  1.1  riastrad 	*p = be32toh(be);
    705  1.1  riastrad 	return 0;
    706  1.1  riastrad }
    707  1.1  riastrad 
    708  1.1  riastrad int
    709  1.1  riastrad apple_smc_write_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
    710  1.1  riastrad     const void *buffer, uint8_t size)
    711  1.1  riastrad {
    712  1.1  riastrad 
    713  1.1  riastrad 	if (key->ask_desc.asd_size != size)
    714  1.1  riastrad 		return EINVAL;
    715  1.1  riastrad 	if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_WRITE))
    716  1.1  riastrad 		return EACCES;
    717  1.1  riastrad 
    718  1.1  riastrad 	return apple_smc_output(smc, APPLE_SMC_CMD_WRITE_KEY, key->ask_name,
    719  1.1  riastrad 	    buffer, size);
    720  1.1  riastrad }
    721  1.1  riastrad 
    722  1.1  riastrad int
    723  1.1  riastrad apple_smc_write_key_1(struct apple_smc_tag *smc,
    724  1.1  riastrad     const struct apple_smc_key *key, uint8_t v)
    725  1.1  riastrad {
    726  1.1  riastrad 
    727  1.1  riastrad 	return apple_smc_write_key(smc, key, &v, 1);
    728  1.1  riastrad }
    729  1.1  riastrad 
    730  1.1  riastrad int
    731  1.1  riastrad apple_smc_write_key_2(struct apple_smc_tag *smc,
    732  1.1  riastrad     const struct apple_smc_key *key, uint16_t v)
    733  1.1  riastrad {
    734  1.1  riastrad 	const uint16_t v_be = htobe16(v);
    735  1.1  riastrad 
    736  1.1  riastrad 	return apple_smc_write_key(smc, key, &v_be, 2);
    737  1.1  riastrad }
    738  1.1  riastrad 
    739  1.1  riastrad int
    740  1.1  riastrad apple_smc_write_key_4(struct apple_smc_tag *smc,
    741  1.2  riastrad     const struct apple_smc_key *key, uint32_t v)
    742  1.2  riastrad {
    743  1.2  riastrad 	const uint16_t v_be = htobe32(v);
    744  1.2  riastrad 
    745  1.2  riastrad 	return apple_smc_write_key(smc, key, &v_be, 4);
    746  1.2  riastrad }
    747  1.2  riastrad 
    748  1.2  riastrad MODULE(MODULE_CLASS_MISC, apple_smc, NULL)
    750  1.2  riastrad 
    751  1.2  riastrad static int
    752  1.2  riastrad apple_smc_modcmd(modcmd_t cmd, void *data __unused)
    753  1.2  riastrad {
    754  1.2  riastrad 
    755  1.2  riastrad 	switch (cmd) {
    756  1.2  riastrad 	case MODULE_CMD_INIT:
    757  1.2  riastrad 		return apple_smc_init();
    758  1.2  riastrad 
    759                	case MODULE_CMD_FINI:
    760                		return apple_smc_fini();
    761                
    762                	default:
    763                		return ENOTTY;
    764                	}
    765                }
    766