1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002 SIXNET, dge@sixnetio.com.
4  *
5  * (C) Copyright 2004, Li-Pro.Net <www.li-pro.net>
6  * Stephan Linz <linz@li-pro.net>
7  */
8 
9 /*
10  * Date & Time support for DS1306 RTC using SPI:
11  *
12  *    - SXNI855T:    it uses its own soft SPI here in this file
13  *    - all other:   use the external spi_xfer() function
14  *                   (see include/spi.h)
15  */
16 
17 #include <common.h>
18 #include <command.h>
19 #include <rtc.h>
20 #include <spi.h>
21 #include <linux/delay.h>
22 
23 #define	RTC_SECONDS		0x00
24 #define	RTC_MINUTES		0x01
25 #define	RTC_HOURS		0x02
26 #define	RTC_DAY_OF_WEEK		0x03
27 #define	RTC_DATE_OF_MONTH	0x04
28 #define	RTC_MONTH		0x05
29 #define	RTC_YEAR		0x06
30 
31 #define	RTC_SECONDS_ALARM0	0x07
32 #define	RTC_MINUTES_ALARM0	0x08
33 #define	RTC_HOURS_ALARM0	0x09
34 #define	RTC_DAY_OF_WEEK_ALARM0	0x0a
35 
36 #define	RTC_SECONDS_ALARM1	0x0b
37 #define	RTC_MINUTES_ALARM1	0x0c
38 #define	RTC_HOURS_ALARM1	0x0d
39 #define	RTC_DAY_OF_WEEK_ALARM1	0x0e
40 
41 #define	RTC_CONTROL		0x0f
42 #define	RTC_STATUS		0x10
43 #define	RTC_TRICKLE_CHARGER	0x11
44 
45 #define	RTC_USER_RAM_BASE	0x20
46 
47 /* ************************************************************************* */
48 #ifdef CONFIG_SXNI855T		/* !!! SHOULD BE CHANGED TO NEW CODE !!! */
49 
50 static void soft_spi_send (unsigned char n);
51 static unsigned char soft_spi_read (void);
52 static void init_spi (void);
53 
54 /*-----------------------------------------------------------------------
55  * Definitions
56  */
57 
58 #define	PB_SPISCK	0x00000002	/* PB 30 */
59 #define PB_SPIMOSI	0x00000004	/* PB 29 */
60 #define PB_SPIMISO	0x00000008	/* PB 28 */
61 #define PB_SPI_CE	0x00010000	/* PB 15 */
62 
63 /* ------------------------------------------------------------------------- */
64 
65 /* read clock time from DS1306 and return it in *tmp */
rtc_get(struct rtc_time * tmp)66 int rtc_get (struct rtc_time *tmp)
67 {
68 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
69 	unsigned char spi_byte;	/* Data Byte */
70 
71 	init_spi ();		/* set port B for software SPI */
72 
73 	/* Now we can enable the DS1306 RTC */
74 	immap->im_cpm.cp_pbdat |= PB_SPI_CE;
75 	udelay(10);
76 
77 	/* Shift out the address (0) of the time in the Clock Chip */
78 	soft_spi_send (0);
79 
80 	/* Put the clock readings into the rtc_time structure */
81 	tmp->tm_sec = bcd2bin (soft_spi_read ());	/* Read seconds */
82 	tmp->tm_min = bcd2bin (soft_spi_read ());	/* Read minutes */
83 
84 	/* Hours are trickier */
85 	spi_byte = soft_spi_read ();	/* Read Hours into temporary value */
86 	if (spi_byte & 0x40) {
87 		/* 12 hour mode bit is set (time is in 1-12 format) */
88 		if (spi_byte & 0x20) {
89 			/* since PM we add 11 to get 0-23 for hours */
90 			tmp->tm_hour = (bcd2bin (spi_byte & 0x1F)) + 11;
91 		} else {
92 			/* since AM we subtract 1 to get 0-23 for hours */
93 			tmp->tm_hour = (bcd2bin (spi_byte & 0x1F)) - 1;
94 		}
95 	} else {
96 		/* Otherwise, 0-23 hour format */
97 		tmp->tm_hour = (bcd2bin (spi_byte & 0x3F));
98 	}
99 
100 	soft_spi_read ();	/* Read and discard Day of week */
101 	tmp->tm_mday = bcd2bin (soft_spi_read ());	/* Read Day of the Month */
102 	tmp->tm_mon = bcd2bin (soft_spi_read ());	/* Read Month */
103 
104 	/* Read Year and convert to this century */
105 	tmp->tm_year = bcd2bin (soft_spi_read ()) + 2000;
106 
107 	/* Now we can disable the DS1306 RTC */
108 	immap->im_cpm.cp_pbdat &= ~PB_SPI_CE;	/* Disable DS1306 Chip */
109 	udelay(10);
110 
111 	rtc_calc_weekday(tmp);	/* Determine the day of week */
112 
113 	debug ("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
114 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
115 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
116 
117 	return 0;
118 }
119 
120 /* ------------------------------------------------------------------------- */
121 
122 /* set clock time in DS1306 RTC and in MPC8xx RTC */
rtc_set(struct rtc_time * tmp)123 int rtc_set (struct rtc_time *tmp)
124 {
125 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
126 
127 	init_spi ();		/* set port B for software SPI */
128 
129 	/* Now we can enable the DS1306 RTC */
130 	immap->im_cpm.cp_pbdat |= PB_SPI_CE;	/* Enable DS1306 Chip */
131 	udelay(10);
132 
133 	/* First disable write protect in the clock chip control register */
134 	soft_spi_send (0x8F);	/* send address of the control register */
135 	soft_spi_send (0x00);	/* send control register contents */
136 
137 	/* Now disable the DS1306 to terminate the write */
138 	immap->im_cpm.cp_pbdat &= ~PB_SPI_CE;
139 	udelay(10);
140 
141 	/* Now enable the DS1306 to initiate a new write */
142 	immap->im_cpm.cp_pbdat |= PB_SPI_CE;
143 	udelay(10);
144 
145 	/* Next, send the address of the clock time write registers */
146 	soft_spi_send (0x80);	/* send address of the first time register */
147 
148 	/* Use Burst Mode to send all of the time data to the clock */
149 	bin2bcd (tmp->tm_sec);
150 	soft_spi_send (bin2bcd (tmp->tm_sec));	/* Send Seconds */
151 	soft_spi_send (bin2bcd (tmp->tm_min));	/* Send Minutes */
152 	soft_spi_send (bin2bcd (tmp->tm_hour));	/* Send Hour */
153 	soft_spi_send (bin2bcd (tmp->tm_wday));	/* Send Day of the Week */
154 	soft_spi_send (bin2bcd (tmp->tm_mday));	/* Send Day of Month */
155 	soft_spi_send (bin2bcd (tmp->tm_mon));	/* Send Month */
156 	soft_spi_send (bin2bcd (tmp->tm_year - 2000));	/* Send Year */
157 
158 	/* Now we can disable the Clock chip to terminate the burst write */
159 	immap->im_cpm.cp_pbdat &= ~PB_SPI_CE;	/* Disable DS1306 Chip */
160 	udelay(10);
161 
162 	/* Now we can enable the Clock chip to initiate a new write */
163 	immap->im_cpm.cp_pbdat |= PB_SPI_CE;	/* Enable DS1306 Chip */
164 	udelay(10);
165 
166 	/* First we Enable write protect in the clock chip control register */
167 	soft_spi_send (0x8F);	/* send address of the control register */
168 	soft_spi_send (0x40);	/* send out Control Register contents */
169 
170 	/* Now disable the DS1306 */
171 	immap->im_cpm.cp_pbdat &= ~PB_SPI_CE;	/*  Disable DS1306 Chip */
172 	udelay(10);
173 
174 	/* Set standard MPC8xx clock to the same time so Linux will
175 	 * see the time even if it doesn't have a DS1306 clock driver.
176 	 * This helps with experimenting with standard kernels.
177 	 */
178 	{
179 		ulong tim;
180 
181 		tim = rtc_mktime(tmp);
182 
183 		immap->im_sitk.sitk_rtck = KAPWR_KEY;
184 		immap->im_sit.sit_rtc = tim;
185 	}
186 
187 	debug ("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
188 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
189 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
190 
191 	return 0;
192 }
193 
194 /* ------------------------------------------------------------------------- */
195 
196 /* Initialize Port B for software SPI */
init_spi(void)197 static void init_spi (void)
198 {
199 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
200 
201 	/* Force output pins to begin at logic 0 */
202 	immap->im_cpm.cp_pbdat &= ~(PB_SPI_CE | PB_SPIMOSI | PB_SPISCK);
203 
204 	/* Set these 3 signals as outputs */
205 	immap->im_cpm.cp_pbdir |= (PB_SPIMOSI | PB_SPI_CE | PB_SPISCK);
206 
207 	immap->im_cpm.cp_pbdir &= ~PB_SPIMISO;	/* Make MISO pin an input */
208 	udelay(10);
209 }
210 
211 /* ------------------------------------------------------------------------- */
212 
213 /* NOTE: soft_spi_send() assumes that the I/O lines are configured already */
soft_spi_send(unsigned char n)214 static void soft_spi_send (unsigned char n)
215 {
216 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
217 	unsigned char bitpos;	/* bit position to receive */
218 	unsigned char i;	/* Loop Control */
219 
220 	/* bit position to send, start with most significant bit */
221 	bitpos = 0x80;
222 
223 	/* Send 8 bits to software SPI */
224 	for (i = 0; i < 8; i++) {	/* Loop for 8 bits */
225 		immap->im_cpm.cp_pbdat |= PB_SPISCK;	/* Raise SCK */
226 
227 		if (n & bitpos)
228 			immap->im_cpm.cp_pbdat |= PB_SPIMOSI;	/* Set MOSI to 1 */
229 		else
230 			immap->im_cpm.cp_pbdat &= ~PB_SPIMOSI;	/* Set MOSI to 0 */
231 		udelay(10);
232 
233 		immap->im_cpm.cp_pbdat &= ~PB_SPISCK;	/* Lower SCK */
234 		udelay(10);
235 
236 		bitpos >>= 1;	/* Shift for next bit position */
237 	}
238 }
239 
240 /* ------------------------------------------------------------------------- */
241 
242 /* NOTE: soft_spi_read() assumes that the I/O lines are configured already */
soft_spi_read(void)243 static unsigned char soft_spi_read (void)
244 {
245 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
246 
247 	unsigned char spi_byte = 0;	/* Return value, assume success */
248 	unsigned char bitpos;	/* bit position to receive */
249 	unsigned char i;	/* Loop Control */
250 
251 	/* bit position to receive, start with most significant bit */
252 	bitpos = 0x80;
253 
254 	/* Read 8 bits here */
255 	for (i = 0; i < 8; i++) {	/* Do 8 bits in loop */
256 		immap->im_cpm.cp_pbdat |= PB_SPISCK;	/* Raise SCK */
257 		udelay(10);
258 		if (immap->im_cpm.cp_pbdat & PB_SPIMISO)	/* Get a bit of data */
259 			spi_byte |= bitpos;	/* Set data accordingly */
260 		immap->im_cpm.cp_pbdat &= ~PB_SPISCK;	/* Lower SCK */
261 		udelay(10);
262 		bitpos >>= 1;	/* Shift for next bit position */
263 	}
264 
265 	return spi_byte;	/* Return the byte read */
266 }
267 
268 /* ------------------------------------------------------------------------- */
269 
rtc_reset(void)270 void rtc_reset (void)
271 {
272 	return;			/* nothing to do */
273 }
274 
275 #else  /* not CONFIG_SXNI855T */
276 /* ************************************************************************* */
277 
278 static unsigned char rtc_read (unsigned char reg);
279 static void rtc_write (unsigned char reg, unsigned char val);
280 
281 static struct spi_slave *slave;
282 
283 /* read clock time from DS1306 and return it in *tmp */
rtc_get(struct rtc_time * tmp)284 int rtc_get (struct rtc_time *tmp)
285 {
286 	unsigned char sec, min, hour, mday, wday, mon, year;
287 
288 	/*
289 	 * Assuming Vcc = 2.0V (lowest speed)
290 	 *
291 	 * REVISIT: If we add an rtc_init() function we can do this
292 	 * step just once.
293 	 */
294 	if (!slave) {
295 		slave = spi_setup_slave(0, CONFIG_SYS_SPI_RTC_DEVID, 600000,
296 				SPI_MODE_3 | SPI_CS_HIGH);
297 		if (!slave)
298 			return;
299 	}
300 
301 	if (spi_claim_bus(slave))
302 		return;
303 
304 	sec = rtc_read (RTC_SECONDS);
305 	min = rtc_read (RTC_MINUTES);
306 	hour = rtc_read (RTC_HOURS);
307 	mday = rtc_read (RTC_DATE_OF_MONTH);
308 	wday = rtc_read (RTC_DAY_OF_WEEK);
309 	mon = rtc_read (RTC_MONTH);
310 	year = rtc_read (RTC_YEAR);
311 
312 	spi_release_bus(slave);
313 
314 	debug ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
315 	       "hr: %02x min: %02x sec: %02x\n",
316 	       year, mon, mday, wday, hour, min, sec);
317 	debug ("Alarms[0]: wday: %02x hour: %02x min: %02x sec: %02x\n",
318 	       rtc_read (RTC_DAY_OF_WEEK_ALARM0),
319 	       rtc_read (RTC_HOURS_ALARM0),
320 	       rtc_read (RTC_MINUTES_ALARM0), rtc_read (RTC_SECONDS_ALARM0));
321 	debug ("Alarms[1]: wday: %02x hour: %02x min: %02x sec: %02x\n",
322 	       rtc_read (RTC_DAY_OF_WEEK_ALARM1),
323 	       rtc_read (RTC_HOURS_ALARM1),
324 	       rtc_read (RTC_MINUTES_ALARM1), rtc_read (RTC_SECONDS_ALARM1));
325 
326 	tmp->tm_sec = bcd2bin (sec & 0x7F);	/* convert Seconds */
327 	tmp->tm_min = bcd2bin (min & 0x7F);	/* convert Minutes */
328 
329 	/* convert Hours */
330 	tmp->tm_hour = (hour & 0x40)
331 		? ((hour & 0x20)	/* 12 hour mode */
332 		   ? bcd2bin (hour & 0x1F) + 11	/* PM */
333 		   : bcd2bin (hour & 0x1F) - 1	/* AM */
334 		)
335 		: bcd2bin (hour & 0x3F);	/* 24 hour mode */
336 
337 	tmp->tm_mday = bcd2bin (mday & 0x3F);	/* convert Day of the Month */
338 	tmp->tm_mon = bcd2bin (mon & 0x1F);	/* convert Month */
339 	tmp->tm_year = bcd2bin (year) + 2000;	/* convert Year */
340 	tmp->tm_wday = bcd2bin (wday & 0x07) - 1;	/* convert Day of the Week */
341 	tmp->tm_yday = 0;
342 	tmp->tm_isdst = 0;
343 
344 	debug ("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
345 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
346 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
347 
348 	return 0;
349 }
350 
351 /* ------------------------------------------------------------------------- */
352 
353 /* set clock time from *tmp in DS1306 RTC */
rtc_set(struct rtc_time * tmp)354 int rtc_set (struct rtc_time *tmp)
355 {
356 	/* Assuming Vcc = 2.0V (lowest speed) */
357 	if (!slave) {
358 		slave = spi_setup_slave(0, CONFIG_SYS_SPI_RTC_DEVID, 600000,
359 				SPI_MODE_3 | SPI_CS_HIGH);
360 		if (!slave)
361 			return;
362 	}
363 
364 	if (spi_claim_bus(slave))
365 		return;
366 
367 	debug ("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
368 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
369 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
370 
371 	rtc_write (RTC_SECONDS, bin2bcd (tmp->tm_sec));
372 	rtc_write (RTC_MINUTES, bin2bcd (tmp->tm_min));
373 	rtc_write (RTC_HOURS, bin2bcd (tmp->tm_hour));
374 	rtc_write (RTC_DAY_OF_WEEK, bin2bcd (tmp->tm_wday + 1));
375 	rtc_write (RTC_DATE_OF_MONTH, bin2bcd (tmp->tm_mday));
376 	rtc_write (RTC_MONTH, bin2bcd (tmp->tm_mon));
377 	rtc_write (RTC_YEAR, bin2bcd (tmp->tm_year - 2000));
378 
379 	spi_release_bus(slave);
380 }
381 
382 /* ------------------------------------------------------------------------- */
383 
384 /* reset the DS1306 */
rtc_reset(void)385 void rtc_reset (void)
386 {
387 	/* Assuming Vcc = 2.0V (lowest speed) */
388 	if (!slave) {
389 		slave = spi_setup_slave(0, CONFIG_SYS_SPI_RTC_DEVID, 600000,
390 				SPI_MODE_3 | SPI_CS_HIGH);
391 		if (!slave)
392 			return;
393 	}
394 
395 	if (spi_claim_bus(slave))
396 		return;
397 
398 	/* clear the control register */
399 	rtc_write (RTC_CONTROL, 0x00);	/* 1st step: reset WP */
400 	rtc_write (RTC_CONTROL, 0x00);	/* 2nd step: reset 1Hz, AIE1, AIE0 */
401 
402 	/* reset all alarms */
403 	rtc_write (RTC_SECONDS_ALARM0, 0x00);
404 	rtc_write (RTC_SECONDS_ALARM1, 0x00);
405 	rtc_write (RTC_MINUTES_ALARM0, 0x00);
406 	rtc_write (RTC_MINUTES_ALARM1, 0x00);
407 	rtc_write (RTC_HOURS_ALARM0, 0x00);
408 	rtc_write (RTC_HOURS_ALARM1, 0x00);
409 	rtc_write (RTC_DAY_OF_WEEK_ALARM0, 0x00);
410 	rtc_write (RTC_DAY_OF_WEEK_ALARM1, 0x00);
411 
412 	spi_release_bus(slave);
413 }
414 
415 /* ------------------------------------------------------------------------- */
416 
rtc_read(unsigned char reg)417 static unsigned char rtc_read (unsigned char reg)
418 {
419 	int ret;
420 
421 	ret = spi_w8r8(slave, reg);
422 	return ret < 0 ? 0 : ret;
423 }
424 
425 /* ------------------------------------------------------------------------- */
426 
rtc_write(unsigned char reg,unsigned char val)427 static void rtc_write (unsigned char reg, unsigned char val)
428 {
429 	unsigned char dout[2];	/* SPI Output Data Bytes */
430 	unsigned char din[2];	/* SPI Input Data Bytes */
431 
432 	dout[0] = 0x80 | reg;
433 	dout[1] = val;
434 
435 	spi_xfer (slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
436 }
437 
438 #endif /* end of code exclusion (see #ifdef CONFIG_SXNI855T above) */
439