xref: /openbmc/linux/drivers/rtc/rtc-gamecube.c (revision 86559400b3ef9de93ba50523cffe767c35cd531a)
1*86559400SEmmanuel Gil Peyrot // SPDX-License-Identifier: GPL-2.0
2*86559400SEmmanuel Gil Peyrot /*
3*86559400SEmmanuel Gil Peyrot  * Nintendo GameCube, Wii and Wii U RTC driver
4*86559400SEmmanuel Gil Peyrot  *
5*86559400SEmmanuel Gil Peyrot  * This driver is for the MX23L4005, more specifically its real-time clock and
6*86559400SEmmanuel Gil Peyrot  * SRAM storage.  The value returned by the RTC counter must be added with the
7*86559400SEmmanuel Gil Peyrot  * offset stored in a bias register in SRAM (on the GameCube and Wii) or in
8*86559400SEmmanuel Gil Peyrot  * /config/rtc.xml (on the Wii U).  The latter being very impractical to access
9*86559400SEmmanuel Gil Peyrot  * from Linux, this driver assumes the bootloader has read it and stored it in
10*86559400SEmmanuel Gil Peyrot  * SRAM like for the other two consoles.
11*86559400SEmmanuel Gil Peyrot  *
12*86559400SEmmanuel Gil Peyrot  * This device sits on a bus named EXI (which is similar to SPI), channel 0,
13*86559400SEmmanuel Gil Peyrot  * device 1.  This driver assumes no other user of the EXI bus, which is
14*86559400SEmmanuel Gil Peyrot  * currently the case but would have to be reworked to add support for other
15*86559400SEmmanuel Gil Peyrot  * GameCube hardware exposed on this bus.
16*86559400SEmmanuel Gil Peyrot  *
17*86559400SEmmanuel Gil Peyrot  * References:
18*86559400SEmmanuel Gil Peyrot  * - https://wiiubrew.org/wiki/Hardware/RTC
19*86559400SEmmanuel Gil Peyrot  * - https://wiibrew.org/wiki/MX23L4005
20*86559400SEmmanuel Gil Peyrot  *
21*86559400SEmmanuel Gil Peyrot  * Copyright (C) 2018 rw-r-r-0644
22*86559400SEmmanuel Gil Peyrot  * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
23*86559400SEmmanuel Gil Peyrot  *
24*86559400SEmmanuel Gil Peyrot  * Based on rtc-gcn.c
25*86559400SEmmanuel Gil Peyrot  * Copyright (C) 2004-2009 The GameCube Linux Team
26*86559400SEmmanuel Gil Peyrot  * Copyright (C) 2005,2008,2009 Albert Herranz
27*86559400SEmmanuel Gil Peyrot  * Based on gamecube_time.c from Torben Nielsen.
28*86559400SEmmanuel Gil Peyrot  */
29*86559400SEmmanuel Gil Peyrot 
30*86559400SEmmanuel Gil Peyrot #include <linux/init.h>
31*86559400SEmmanuel Gil Peyrot #include <linux/module.h>
32*86559400SEmmanuel Gil Peyrot #include <linux/of.h>
33*86559400SEmmanuel Gil Peyrot #include <linux/of_address.h>
34*86559400SEmmanuel Gil Peyrot #include <linux/platform_device.h>
35*86559400SEmmanuel Gil Peyrot #include <linux/regmap.h>
36*86559400SEmmanuel Gil Peyrot #include <linux/rtc.h>
37*86559400SEmmanuel Gil Peyrot #include <linux/time.h>
38*86559400SEmmanuel Gil Peyrot 
39*86559400SEmmanuel Gil Peyrot /* EXI registers */
40*86559400SEmmanuel Gil Peyrot #define EXICSR	0
41*86559400SEmmanuel Gil Peyrot #define EXICR	12
42*86559400SEmmanuel Gil Peyrot #define EXIDATA	16
43*86559400SEmmanuel Gil Peyrot 
44*86559400SEmmanuel Gil Peyrot /* EXI register values */
45*86559400SEmmanuel Gil Peyrot #define EXICSR_DEV		0x380
46*86559400SEmmanuel Gil Peyrot 	#define EXICSR_DEV1	0x100
47*86559400SEmmanuel Gil Peyrot #define EXICSR_CLK		0x070
48*86559400SEmmanuel Gil Peyrot 	#define EXICSR_CLK_1MHZ	0x000
49*86559400SEmmanuel Gil Peyrot 	#define EXICSR_CLK_2MHZ	0x010
50*86559400SEmmanuel Gil Peyrot 	#define EXICSR_CLK_4MHZ	0x020
51*86559400SEmmanuel Gil Peyrot 	#define EXICSR_CLK_8MHZ	0x030
52*86559400SEmmanuel Gil Peyrot 	#define EXICSR_CLK_16MHZ 0x040
53*86559400SEmmanuel Gil Peyrot 	#define EXICSR_CLK_32MHZ 0x050
54*86559400SEmmanuel Gil Peyrot #define EXICSR_INT		0x008
55*86559400SEmmanuel Gil Peyrot 	#define EXICSR_INTSET	0x008
56*86559400SEmmanuel Gil Peyrot 
57*86559400SEmmanuel Gil Peyrot #define EXICR_TSTART		0x001
58*86559400SEmmanuel Gil Peyrot #define EXICR_TRSMODE		0x002
59*86559400SEmmanuel Gil Peyrot 	#define EXICR_TRSMODE_IMM 0x000
60*86559400SEmmanuel Gil Peyrot #define EXICR_TRSTYPE		0x00C
61*86559400SEmmanuel Gil Peyrot 	#define EXICR_TRSTYPE_R	0x000
62*86559400SEmmanuel Gil Peyrot 	#define EXICR_TRSTYPE_W	0x004
63*86559400SEmmanuel Gil Peyrot #define EXICR_TLEN		0x030
64*86559400SEmmanuel Gil Peyrot 	#define EXICR_TLEN32	0x030
65*86559400SEmmanuel Gil Peyrot 
66*86559400SEmmanuel Gil Peyrot /* EXI registers values to access the RTC */
67*86559400SEmmanuel Gil Peyrot #define RTC_EXICSR	(EXICSR_DEV1 | EXICSR_CLK_8MHZ | EXICSR_INTSET)
68*86559400SEmmanuel Gil Peyrot #define RTC_EXICR_W	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_W | EXICR_TLEN32)
69*86559400SEmmanuel Gil Peyrot #define RTC_EXICR_R	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_R | EXICR_TLEN32)
70*86559400SEmmanuel Gil Peyrot #define RTC_EXIDATA_W	0x80000000
71*86559400SEmmanuel Gil Peyrot 
72*86559400SEmmanuel Gil Peyrot /* RTC registers */
73*86559400SEmmanuel Gil Peyrot #define RTC_COUNTER	0x200000
74*86559400SEmmanuel Gil Peyrot #define RTC_SRAM	0x200001
75*86559400SEmmanuel Gil Peyrot #define RTC_SRAM_BIAS	0x200004
76*86559400SEmmanuel Gil Peyrot #define RTC_SNAPSHOT	0x204000
77*86559400SEmmanuel Gil Peyrot #define RTC_ONTMR	0x210000
78*86559400SEmmanuel Gil Peyrot #define RTC_OFFTMR	0x210001
79*86559400SEmmanuel Gil Peyrot #define RTC_TEST0	0x210004
80*86559400SEmmanuel Gil Peyrot #define RTC_TEST1	0x210005
81*86559400SEmmanuel Gil Peyrot #define RTC_TEST2	0x210006
82*86559400SEmmanuel Gil Peyrot #define RTC_TEST3	0x210007
83*86559400SEmmanuel Gil Peyrot #define RTC_CONTROL0	0x21000c
84*86559400SEmmanuel Gil Peyrot #define RTC_CONTROL1	0x21000d
85*86559400SEmmanuel Gil Peyrot 
86*86559400SEmmanuel Gil Peyrot struct priv {
87*86559400SEmmanuel Gil Peyrot 	struct regmap *regmap;
88*86559400SEmmanuel Gil Peyrot 	void __iomem *iob;
89*86559400SEmmanuel Gil Peyrot 	u32 rtc_bias;
90*86559400SEmmanuel Gil Peyrot };
91*86559400SEmmanuel Gil Peyrot 
92*86559400SEmmanuel Gil Peyrot static int exi_read(void *context, u32 reg, u32 *data)
93*86559400SEmmanuel Gil Peyrot {
94*86559400SEmmanuel Gil Peyrot 	struct priv *d = (struct priv *)context;
95*86559400SEmmanuel Gil Peyrot 	void __iomem *iob = d->iob;
96*86559400SEmmanuel Gil Peyrot 
97*86559400SEmmanuel Gil Peyrot 	/* The spin loops here loop about 15~16 times each, so there is no need
98*86559400SEmmanuel Gil Peyrot 	 * to use a more expensive sleep method.
99*86559400SEmmanuel Gil Peyrot 	 */
100*86559400SEmmanuel Gil Peyrot 
101*86559400SEmmanuel Gil Peyrot 	/* Write register offset */
102*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICSR, iob + EXICSR);
103*86559400SEmmanuel Gil Peyrot 	iowrite32be(reg << 8, iob + EXIDATA);
104*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICR_W, iob + EXICR);
105*86559400SEmmanuel Gil Peyrot 	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
106*86559400SEmmanuel Gil Peyrot 		cpu_relax();
107*86559400SEmmanuel Gil Peyrot 
108*86559400SEmmanuel Gil Peyrot 	/* Read data */
109*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICSR, iob + EXICSR);
110*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICR_R, iob + EXICR);
111*86559400SEmmanuel Gil Peyrot 	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
112*86559400SEmmanuel Gil Peyrot 		cpu_relax();
113*86559400SEmmanuel Gil Peyrot 	*data = ioread32be(iob + EXIDATA);
114*86559400SEmmanuel Gil Peyrot 
115*86559400SEmmanuel Gil Peyrot 	/* Clear channel parameters */
116*86559400SEmmanuel Gil Peyrot 	iowrite32be(0, iob + EXICSR);
117*86559400SEmmanuel Gil Peyrot 
118*86559400SEmmanuel Gil Peyrot 	return 0;
119*86559400SEmmanuel Gil Peyrot }
120*86559400SEmmanuel Gil Peyrot 
121*86559400SEmmanuel Gil Peyrot static int exi_write(void *context, u32 reg, u32 data)
122*86559400SEmmanuel Gil Peyrot {
123*86559400SEmmanuel Gil Peyrot 	struct priv *d = (struct priv *)context;
124*86559400SEmmanuel Gil Peyrot 	void __iomem *iob = d->iob;
125*86559400SEmmanuel Gil Peyrot 
126*86559400SEmmanuel Gil Peyrot 	/* The spin loops here loop about 15~16 times each, so there is no need
127*86559400SEmmanuel Gil Peyrot 	 * to use a more expensive sleep method.
128*86559400SEmmanuel Gil Peyrot 	 */
129*86559400SEmmanuel Gil Peyrot 
130*86559400SEmmanuel Gil Peyrot 	/* Write register offset */
131*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICSR, iob + EXICSR);
132*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXIDATA_W | (reg << 8), iob + EXIDATA);
133*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICR_W, iob + EXICR);
134*86559400SEmmanuel Gil Peyrot 	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
135*86559400SEmmanuel Gil Peyrot 		cpu_relax();
136*86559400SEmmanuel Gil Peyrot 
137*86559400SEmmanuel Gil Peyrot 	/* Write data */
138*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICSR, iob + EXICSR);
139*86559400SEmmanuel Gil Peyrot 	iowrite32be(data, iob + EXIDATA);
140*86559400SEmmanuel Gil Peyrot 	iowrite32be(RTC_EXICR_W, iob + EXICR);
141*86559400SEmmanuel Gil Peyrot 	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
142*86559400SEmmanuel Gil Peyrot 		cpu_relax();
143*86559400SEmmanuel Gil Peyrot 
144*86559400SEmmanuel Gil Peyrot 	/* Clear channel parameters */
145*86559400SEmmanuel Gil Peyrot 	iowrite32be(0, iob + EXICSR);
146*86559400SEmmanuel Gil Peyrot 
147*86559400SEmmanuel Gil Peyrot 	return 0;
148*86559400SEmmanuel Gil Peyrot }
149*86559400SEmmanuel Gil Peyrot 
150*86559400SEmmanuel Gil Peyrot static const struct regmap_bus exi_bus = {
151*86559400SEmmanuel Gil Peyrot 	/* TODO: is that true?  Not that it matters here, but still. */
152*86559400SEmmanuel Gil Peyrot 	.fast_io = true,
153*86559400SEmmanuel Gil Peyrot 	.reg_read = exi_read,
154*86559400SEmmanuel Gil Peyrot 	.reg_write = exi_write,
155*86559400SEmmanuel Gil Peyrot };
156*86559400SEmmanuel Gil Peyrot 
157*86559400SEmmanuel Gil Peyrot static int gamecube_rtc_read_time(struct device *dev, struct rtc_time *t)
158*86559400SEmmanuel Gil Peyrot {
159*86559400SEmmanuel Gil Peyrot 	struct priv *d = dev_get_drvdata(dev);
160*86559400SEmmanuel Gil Peyrot 	int ret;
161*86559400SEmmanuel Gil Peyrot 	u32 counter;
162*86559400SEmmanuel Gil Peyrot 	time64_t timestamp;
163*86559400SEmmanuel Gil Peyrot 
164*86559400SEmmanuel Gil Peyrot 	ret = regmap_read(d->regmap, RTC_COUNTER, &counter);
165*86559400SEmmanuel Gil Peyrot 	if (ret)
166*86559400SEmmanuel Gil Peyrot 		return ret;
167*86559400SEmmanuel Gil Peyrot 
168*86559400SEmmanuel Gil Peyrot 	/* Add the counter and the bias to obtain the timestamp */
169*86559400SEmmanuel Gil Peyrot 	timestamp = (time64_t)d->rtc_bias + counter;
170*86559400SEmmanuel Gil Peyrot 	rtc_time64_to_tm(timestamp, t);
171*86559400SEmmanuel Gil Peyrot 
172*86559400SEmmanuel Gil Peyrot 	return 0;
173*86559400SEmmanuel Gil Peyrot }
174*86559400SEmmanuel Gil Peyrot 
175*86559400SEmmanuel Gil Peyrot static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
176*86559400SEmmanuel Gil Peyrot {
177*86559400SEmmanuel Gil Peyrot 	struct priv *d = dev_get_drvdata(dev);
178*86559400SEmmanuel Gil Peyrot 	time64_t timestamp;
179*86559400SEmmanuel Gil Peyrot 
180*86559400SEmmanuel Gil Peyrot 	/* Subtract the timestamp and the bias to obtain the counter value */
181*86559400SEmmanuel Gil Peyrot 	timestamp = rtc_tm_to_time64(t);
182*86559400SEmmanuel Gil Peyrot 	return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
183*86559400SEmmanuel Gil Peyrot }
184*86559400SEmmanuel Gil Peyrot 
185*86559400SEmmanuel Gil Peyrot static const struct rtc_class_ops gamecube_rtc_ops = {
186*86559400SEmmanuel Gil Peyrot 	.read_time	= gamecube_rtc_read_time,
187*86559400SEmmanuel Gil Peyrot 	.set_time	= gamecube_rtc_set_time,
188*86559400SEmmanuel Gil Peyrot };
189*86559400SEmmanuel Gil Peyrot 
190*86559400SEmmanuel Gil Peyrot static int gamecube_rtc_read_offset_from_sram(struct priv *d)
191*86559400SEmmanuel Gil Peyrot {
192*86559400SEmmanuel Gil Peyrot 	struct device_node *np;
193*86559400SEmmanuel Gil Peyrot 	int ret;
194*86559400SEmmanuel Gil Peyrot 	struct resource res;
195*86559400SEmmanuel Gil Peyrot 	void __iomem *hw_srnprot;
196*86559400SEmmanuel Gil Peyrot 	u32 old;
197*86559400SEmmanuel Gil Peyrot 
198*86559400SEmmanuel Gil Peyrot 	np = of_find_compatible_node(NULL, NULL, "nintendo,latte-srnprot");
199*86559400SEmmanuel Gil Peyrot 	if (!np)
200*86559400SEmmanuel Gil Peyrot 		np = of_find_compatible_node(NULL, NULL,
201*86559400SEmmanuel Gil Peyrot 					     "nintendo,hollywood-srnprot");
202*86559400SEmmanuel Gil Peyrot 	if (!np) {
203*86559400SEmmanuel Gil Peyrot 		pr_info("HW_SRNPROT not found, assuming a GameCube\n");
204*86559400SEmmanuel Gil Peyrot 		return regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
205*86559400SEmmanuel Gil Peyrot 	}
206*86559400SEmmanuel Gil Peyrot 
207*86559400SEmmanuel Gil Peyrot 	ret = of_address_to_resource(np, 0, &res);
208*86559400SEmmanuel Gil Peyrot 	if (ret) {
209*86559400SEmmanuel Gil Peyrot 		pr_err("no io memory range found\n");
210*86559400SEmmanuel Gil Peyrot 		return -1;
211*86559400SEmmanuel Gil Peyrot 	}
212*86559400SEmmanuel Gil Peyrot 
213*86559400SEmmanuel Gil Peyrot 	hw_srnprot = ioremap(res.start, resource_size(&res));
214*86559400SEmmanuel Gil Peyrot 	old = ioread32be(hw_srnprot);
215*86559400SEmmanuel Gil Peyrot 
216*86559400SEmmanuel Gil Peyrot 	/* TODO: figure out why we use this magic constant.  I obtained it by
217*86559400SEmmanuel Gil Peyrot 	 * reading the leftover value after boot, after IOSU already ran.
218*86559400SEmmanuel Gil Peyrot 	 *
219*86559400SEmmanuel Gil Peyrot 	 * On my Wii U, setting this register to 1 prevents the console from
220*86559400SEmmanuel Gil Peyrot 	 * rebooting properly, so wiiubrew.org must be missing something.
221*86559400SEmmanuel Gil Peyrot 	 *
222*86559400SEmmanuel Gil Peyrot 	 * See https://wiiubrew.org/wiki/Hardware/Latte_registers
223*86559400SEmmanuel Gil Peyrot 	 */
224*86559400SEmmanuel Gil Peyrot 	if (old != 0x7bf)
225*86559400SEmmanuel Gil Peyrot 		iowrite32be(0x7bf, hw_srnprot);
226*86559400SEmmanuel Gil Peyrot 
227*86559400SEmmanuel Gil Peyrot 	/* Get the offset from RTC SRAM.
228*86559400SEmmanuel Gil Peyrot 	 *
229*86559400SEmmanuel Gil Peyrot 	 * Its default location on the GameCube and on the Wii is in the SRAM,
230*86559400SEmmanuel Gil Peyrot 	 * while on the Wii U the bootloader needs to fill it with the contents
231*86559400SEmmanuel Gil Peyrot 	 * of /config/rtc.xml on the SLC (the eMMC).  We don’t do that from
232*86559400SEmmanuel Gil Peyrot 	 * Linux since it requires implementing a proprietary filesystem and do
233*86559400SEmmanuel Gil Peyrot 	 * file decryption, instead we require the bootloader to fill the same
234*86559400SEmmanuel Gil Peyrot 	 * SRAM address as on previous consoles.
235*86559400SEmmanuel Gil Peyrot 	 */
236*86559400SEmmanuel Gil Peyrot 	ret = regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
237*86559400SEmmanuel Gil Peyrot 	if (ret) {
238*86559400SEmmanuel Gil Peyrot 		pr_err("failed to get the RTC bias\n");
239*86559400SEmmanuel Gil Peyrot 		return -1;
240*86559400SEmmanuel Gil Peyrot 	}
241*86559400SEmmanuel Gil Peyrot 
242*86559400SEmmanuel Gil Peyrot 	/* Reset SRAM access to how it was before, our job here is done. */
243*86559400SEmmanuel Gil Peyrot 	if (old != 0x7bf)
244*86559400SEmmanuel Gil Peyrot 		iowrite32be(old, hw_srnprot);
245*86559400SEmmanuel Gil Peyrot 	iounmap(hw_srnprot);
246*86559400SEmmanuel Gil Peyrot 
247*86559400SEmmanuel Gil Peyrot 	return 0;
248*86559400SEmmanuel Gil Peyrot }
249*86559400SEmmanuel Gil Peyrot 
250*86559400SEmmanuel Gil Peyrot static const struct regmap_range rtc_rd_ranges[] = {
251*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x200000, 0x200010),
252*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x204000, 0x204000),
253*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x210000, 0x210001),
254*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x210004, 0x210007),
255*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x21000c, 0x21000d),
256*86559400SEmmanuel Gil Peyrot };
257*86559400SEmmanuel Gil Peyrot 
258*86559400SEmmanuel Gil Peyrot static const struct regmap_access_table rtc_rd_regs = {
259*86559400SEmmanuel Gil Peyrot 	.yes_ranges =	rtc_rd_ranges,
260*86559400SEmmanuel Gil Peyrot 	.n_yes_ranges =	ARRAY_SIZE(rtc_rd_ranges),
261*86559400SEmmanuel Gil Peyrot };
262*86559400SEmmanuel Gil Peyrot 
263*86559400SEmmanuel Gil Peyrot static const struct regmap_range rtc_wr_ranges[] = {
264*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x200000, 0x200010),
265*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x204000, 0x204000),
266*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x210000, 0x210001),
267*86559400SEmmanuel Gil Peyrot 	regmap_reg_range(0x21000d, 0x21000d),
268*86559400SEmmanuel Gil Peyrot };
269*86559400SEmmanuel Gil Peyrot 
270*86559400SEmmanuel Gil Peyrot static const struct regmap_access_table rtc_wr_regs = {
271*86559400SEmmanuel Gil Peyrot 	.yes_ranges =	rtc_wr_ranges,
272*86559400SEmmanuel Gil Peyrot 	.n_yes_ranges =	ARRAY_SIZE(rtc_wr_ranges),
273*86559400SEmmanuel Gil Peyrot };
274*86559400SEmmanuel Gil Peyrot 
275*86559400SEmmanuel Gil Peyrot static const struct regmap_config gamecube_rtc_regmap_config = {
276*86559400SEmmanuel Gil Peyrot 	.reg_bits = 24,
277*86559400SEmmanuel Gil Peyrot 	.val_bits = 32,
278*86559400SEmmanuel Gil Peyrot 	.rd_table = &rtc_rd_regs,
279*86559400SEmmanuel Gil Peyrot 	.wr_table = &rtc_wr_regs,
280*86559400SEmmanuel Gil Peyrot 	.max_register = 0x21000d,
281*86559400SEmmanuel Gil Peyrot 	.name = "gamecube-rtc",
282*86559400SEmmanuel Gil Peyrot };
283*86559400SEmmanuel Gil Peyrot 
284*86559400SEmmanuel Gil Peyrot static int gamecube_rtc_probe(struct platform_device *pdev)
285*86559400SEmmanuel Gil Peyrot {
286*86559400SEmmanuel Gil Peyrot 	struct device *dev = &pdev->dev;
287*86559400SEmmanuel Gil Peyrot 	struct rtc_device *rtc;
288*86559400SEmmanuel Gil Peyrot 	struct priv *d;
289*86559400SEmmanuel Gil Peyrot 	int ret;
290*86559400SEmmanuel Gil Peyrot 
291*86559400SEmmanuel Gil Peyrot 	d = devm_kzalloc(dev, sizeof(struct priv), GFP_KERNEL);
292*86559400SEmmanuel Gil Peyrot 	if (IS_ERR(d))
293*86559400SEmmanuel Gil Peyrot 		return PTR_ERR(d);
294*86559400SEmmanuel Gil Peyrot 
295*86559400SEmmanuel Gil Peyrot 	d->iob = devm_platform_ioremap_resource(pdev, 0);
296*86559400SEmmanuel Gil Peyrot 	if (IS_ERR(d->iob))
297*86559400SEmmanuel Gil Peyrot 		return PTR_ERR(d->iob);
298*86559400SEmmanuel Gil Peyrot 
299*86559400SEmmanuel Gil Peyrot 	d->regmap = devm_regmap_init(dev, &exi_bus, d,
300*86559400SEmmanuel Gil Peyrot 				     &gamecube_rtc_regmap_config);
301*86559400SEmmanuel Gil Peyrot 	if (IS_ERR(d->regmap))
302*86559400SEmmanuel Gil Peyrot 		return PTR_ERR(d->regmap);
303*86559400SEmmanuel Gil Peyrot 
304*86559400SEmmanuel Gil Peyrot 	ret = gamecube_rtc_read_offset_from_sram(d);
305*86559400SEmmanuel Gil Peyrot 	if (ret)
306*86559400SEmmanuel Gil Peyrot 		return ret;
307*86559400SEmmanuel Gil Peyrot 	dev_dbg(dev, "SRAM bias: 0x%x", d->rtc_bias);
308*86559400SEmmanuel Gil Peyrot 
309*86559400SEmmanuel Gil Peyrot 	dev_set_drvdata(dev, d);
310*86559400SEmmanuel Gil Peyrot 
311*86559400SEmmanuel Gil Peyrot 	rtc = devm_rtc_allocate_device(dev);
312*86559400SEmmanuel Gil Peyrot 	if (IS_ERR(rtc))
313*86559400SEmmanuel Gil Peyrot 		return PTR_ERR(rtc);
314*86559400SEmmanuel Gil Peyrot 
315*86559400SEmmanuel Gil Peyrot 	/* We can represent further than that, but it depends on the stored
316*86559400SEmmanuel Gil Peyrot 	 * bias and we can’t modify it persistently on all supported consoles,
317*86559400SEmmanuel Gil Peyrot 	 * so here we pretend to be limited to 2106.
318*86559400SEmmanuel Gil Peyrot 	 */
319*86559400SEmmanuel Gil Peyrot 	rtc->range_min = 0;
320*86559400SEmmanuel Gil Peyrot 	rtc->range_max = U32_MAX;
321*86559400SEmmanuel Gil Peyrot 	rtc->ops = &gamecube_rtc_ops;
322*86559400SEmmanuel Gil Peyrot 
323*86559400SEmmanuel Gil Peyrot 	devm_rtc_register_device(rtc);
324*86559400SEmmanuel Gil Peyrot 
325*86559400SEmmanuel Gil Peyrot 	return 0;
326*86559400SEmmanuel Gil Peyrot }
327*86559400SEmmanuel Gil Peyrot 
328*86559400SEmmanuel Gil Peyrot static const struct of_device_id gamecube_rtc_of_match[] = {
329*86559400SEmmanuel Gil Peyrot 	{.compatible = "nintendo,latte-exi" },
330*86559400SEmmanuel Gil Peyrot 	{.compatible = "nintendo,hollywood-exi" },
331*86559400SEmmanuel Gil Peyrot 	{.compatible = "nintendo,flipper-exi" },
332*86559400SEmmanuel Gil Peyrot 	{ }
333*86559400SEmmanuel Gil Peyrot };
334*86559400SEmmanuel Gil Peyrot MODULE_DEVICE_TABLE(of, gamecube_rtc_of_match);
335*86559400SEmmanuel Gil Peyrot 
336*86559400SEmmanuel Gil Peyrot static struct platform_driver gamecube_rtc_driver = {
337*86559400SEmmanuel Gil Peyrot 	.probe		= gamecube_rtc_probe,
338*86559400SEmmanuel Gil Peyrot 	.driver		= {
339*86559400SEmmanuel Gil Peyrot 		.name	= "rtc-gamecube",
340*86559400SEmmanuel Gil Peyrot 		.of_match_table	= gamecube_rtc_of_match,
341*86559400SEmmanuel Gil Peyrot 	},
342*86559400SEmmanuel Gil Peyrot };
343*86559400SEmmanuel Gil Peyrot module_platform_driver(gamecube_rtc_driver);
344*86559400SEmmanuel Gil Peyrot 
345*86559400SEmmanuel Gil Peyrot MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
346*86559400SEmmanuel Gil Peyrot MODULE_DESCRIPTION("Nintendo GameCube, Wii and Wii U RTC driver");
347*86559400SEmmanuel Gil Peyrot MODULE_LICENSE("GPL");
348