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