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