Home | History | Annotate | Line # | Download | only in mpc85xx
      1  1.2  matt /*	$NetBSD: ds1553rtc.c,v 1.2 2011/01/18 01:10:25 matt Exp $	*/
      2  1.2  matt /*-
      3  1.2  matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  1.2  matt  * All rights reserved.
      5  1.2  matt  *
      6  1.2  matt  * This code is derived from software contributed to The NetBSD Foundation
      7  1.2  matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  1.2  matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  1.2  matt  *
     10  1.2  matt  * This material is based upon work supported by the Defense Advanced Research
     11  1.2  matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  1.2  matt  * Contract No. N66001-09-C-2073.
     13  1.2  matt  * Approved for Public Release, Distribution Unlimited
     14  1.2  matt  *
     15  1.2  matt  * Redistribution and use in source and binary forms, with or without
     16  1.2  matt  * modification, are permitted provided that the following conditions
     17  1.2  matt  * are met:
     18  1.2  matt  * 1. Redistributions of source code must retain the above copyright
     19  1.2  matt  *    notice, this list of conditions and the following disclaimer.
     20  1.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.2  matt  *    notice, this list of conditions and the following disclaimer in the
     22  1.2  matt  *    documentation and/or other materials provided with the distribution.
     23  1.2  matt  *
     24  1.2  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.2  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.2  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.2  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.2  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.2  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.2  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.2  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.2  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.2  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.2  matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.2  matt  */
     36  1.2  matt 
     37  1.2  matt #include "locators.h"
     38  1.2  matt 
     39  1.2  matt #include <sys/cdefs.h>
     40  1.2  matt 
     41  1.2  matt __KERNEL_RCSID(0, "$NetBSD: ds1553rtc.c,v 1.2 2011/01/18 01:10:25 matt Exp $");
     42  1.2  matt 
     43  1.2  matt #include <sys/param.h>
     44  1.2  matt #include <sys/cpu.h>
     45  1.2  matt #include <sys/device.h>
     46  1.2  matt #include <sys/tty.h>
     47  1.2  matt 
     48  1.2  matt #include "ioconf.h"
     49  1.2  matt 
     50  1.2  matt #include <sys/intr.h>
     51  1.2  matt #include <sys/bus.h>
     52  1.2  matt 
     53  1.2  matt #include <dev/clock_subr.h>
     54  1.2  matt #include <dev/ic/mk48txxvar.h>
     55  1.2  matt 
     56  1.2  matt #include <powerpc/booke/cpuvar.h>
     57  1.2  matt 
     58  1.2  matt static int ds1553rtc_match(device_t, cfdata_t, void *);
     59  1.2  matt static void ds1553rtc_attach(device_t, device_t, void *);
     60  1.2  matt 
     61  1.2  matt CFATTACH_DECL_NEW(ds1553rtc, sizeof(struct mk48txx_softc),
     62  1.2  matt     ds1553rtc_match, ds1553rtc_attach, NULL, NULL);
     63  1.2  matt 
     64  1.2  matt static int
     65  1.2  matt ds1553rtc_match(device_t parent, cfdata_t cf, void *aux)
     66  1.2  matt {
     67  1.2  matt 	struct generic_attach_args * const ga = aux;
     68  1.2  matt 	bus_space_handle_t bsh;
     69  1.2  matt 	int error;
     70  1.2  matt 	int rv;
     71  1.2  matt 	uint8_t saved_data, new_data;
     72  1.2  matt 	bus_size_t size = ga->ga_size;
     73  1.2  matt 
     74  1.2  matt 	if (ga->ga_addr == OBIOCF_ADDR_DEFAULT)
     75  1.2  matt 		return 0;
     76  1.2  matt 	if (size == OBIOCF_SIZE_DEFAULT)
     77  1.2  matt 		size = 8192;
     78  1.2  matt 
     79  1.2  matt 	/*
     80  1.2  matt 	 * If this is NVRAM+RTC, make sure we can modify the NVRAM.
     81  1.2  matt 	 */
     82  1.2  matt 	error = bus_space_map(ga->ga_bst, ga->ga_addr, size, 0, &bsh);
     83  1.2  matt 	if (error)
     84  1.2  matt 		return 0;
     85  1.2  matt 
     86  1.2  matt 	bus_size_t target = size - 17;
     87  1.2  matt 	saved_data = bus_space_read_1(ga->ga_bst, bsh, target);
     88  1.2  matt 	bus_space_write_1(ga->ga_bst, bsh, target, ~saved_data);
     89  1.2  matt 	new_data = bus_space_read_1(ga->ga_bst, bsh, target);
     90  1.2  matt 	rv = (new_data == (uint8_t) ~saved_data);
     91  1.2  matt 	bus_space_write_1(ga->ga_bst, bsh, target, saved_data);
     92  1.2  matt 
     93  1.2  matt 	bus_space_unmap(ga->ga_bst, bsh, size);
     94  1.2  matt 
     95  1.2  matt 	return rv;
     96  1.2  matt }
     97  1.2  matt 
     98  1.2  matt static void
     99  1.2  matt ds1553rtc_attach(device_t parent, device_t self, void *aux)
    100  1.2  matt {
    101  1.2  matt 	struct mk48txx_softc * const sc = device_private(self);
    102  1.2  matt 	struct generic_attach_args * const ga = aux;
    103  1.2  matt 	int error;
    104  1.2  matt 	bus_size_t size = ga->ga_size;
    105  1.2  matt 
    106  1.2  matt 	if (size == OBIOCF_SIZE_DEFAULT)
    107  1.2  matt 		size = 8192;
    108  1.2  matt 
    109  1.2  matt 	sc->sc_dev = self;
    110  1.2  matt 	sc->sc_bst = ga->ga_bst;
    111  1.2  matt 	sc->sc_model = "ds1553";
    112  1.2  matt 	sc->sc_flag = MK48TXX_HAVE_CENT_REG;
    113  1.2  matt 	error = bus_space_map(sc->sc_bst, ga->ga_addr, size, 0, &sc->sc_bsh);
    114  1.2  matt 	if (error) {
    115  1.2  matt 		aprint_error(": failed to map registers: %d\n", error);
    116  1.2  matt 		return;
    117  1.2  matt 	}
    118  1.2  matt 
    119  1.2  matt 	mk48txx_attach(sc);
    120  1.2  matt 	aprint_normal("\n");
    121  1.2  matt }
    122