| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * M41T80 serial rtc emulation | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2018 BALATON Zoltan | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the GNU GPL license version 2 or later. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "qemu/osdep.h"
 | 
					
						
							|  |  |  | #include "qemu/log.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | #include "qemu/module.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  | #include "qemu/timer.h"
 | 
					
						
							|  |  |  | #include "qemu/bcd.h"
 | 
					
						
							|  |  |  | #include "hw/i2c/i2c.h"
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | #include "qom/object.h"
 | 
					
						
							| 
									
										
										
										
											2021-11-29 20:55:05 +00:00
										 |  |  | #include "sysemu/rtc.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define TYPE_M41T80 "m41t80"
 | 
					
						
							| 
									
										
										
										
											2020-09-16 14:25:19 -04:00
										 |  |  | OBJECT_DECLARE_SIMPLE_TYPE(M41t80State, M41T80) | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | struct M41t80State { | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  |     I2CSlave parent_obj; | 
					
						
							|  |  |  |     int8_t addr; | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void m41t80_realize(DeviceState *dev, Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     M41t80State *s = M41T80(dev); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s->addr = -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int m41t80_send(I2CSlave *i2c, uint8_t data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     M41t80State *s = M41T80(i2c); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->addr < 0) { | 
					
						
							|  |  |  |         s->addr = data; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         s->addr++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 11:50:50 -06:00
										 |  |  | static uint8_t m41t80_recv(I2CSlave *i2c) | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     M41t80State *s = M41T80(i2c); | 
					
						
							|  |  |  |     struct tm now; | 
					
						
							| 
									
										
										
										
											2022-03-07 11:04:00 +04:00
										 |  |  |     int64_t rt; | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (s->addr < 0) { | 
					
						
							|  |  |  |         s->addr = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (s->addr >= 1 && s->addr <= 7) { | 
					
						
							|  |  |  |         qemu_get_timedate(&now, -1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     switch (s->addr++) { | 
					
						
							|  |  |  |     case 0: | 
					
						
							| 
									
										
										
										
											2022-03-07 11:04:00 +04:00
										 |  |  |         rt = g_get_real_time(); | 
					
						
							|  |  |  |         return to_bcd((rt % G_USEC_PER_SEC) / 10000); | 
					
						
							| 
									
										
										
										
											2018-06-29 00:38:33 +02:00
										 |  |  |     case 1: | 
					
						
							|  |  |  |         return to_bcd(now.tm_sec); | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         return to_bcd(now.tm_min); | 
					
						
							|  |  |  |     case 3: | 
					
						
							|  |  |  |         return to_bcd(now.tm_hour); | 
					
						
							|  |  |  |     case 4: | 
					
						
							|  |  |  |         return to_bcd(now.tm_wday); | 
					
						
							|  |  |  |     case 5: | 
					
						
							|  |  |  |         return to_bcd(now.tm_mday); | 
					
						
							|  |  |  |     case 6: | 
					
						
							|  |  |  |         return to_bcd(now.tm_mon + 1); | 
					
						
							|  |  |  |     case 7: | 
					
						
							|  |  |  |         return to_bcd(now.tm_year % 100); | 
					
						
							|  |  |  |     case 8 ... 19: | 
					
						
							|  |  |  |         qemu_log_mask(LOG_UNIMP, "%s: unimplemented register: %d\n", | 
					
						
							|  |  |  |                       __func__, s->addr - 1); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid register: %d\n", | 
					
						
							|  |  |  |                       __func__, s->addr - 1); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int m41t80_event(I2CSlave *i2c, enum i2c_event event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     M41t80State *s = M41T80(i2c); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (event == I2C_START_SEND) { | 
					
						
							|  |  |  |         s->addr = -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void m41t80_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceClass *dc = DEVICE_CLASS(klass); | 
					
						
							|  |  |  |     I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dc->realize = m41t80_realize; | 
					
						
							|  |  |  |     sc->send = m41t80_send; | 
					
						
							|  |  |  |     sc->recv = m41t80_recv; | 
					
						
							|  |  |  |     sc->event = m41t80_event; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo m41t80_info = { | 
					
						
							|  |  |  |     .name          = TYPE_M41T80, | 
					
						
							|  |  |  |     .parent        = TYPE_I2C_SLAVE, | 
					
						
							|  |  |  |     .instance_size = sizeof(M41t80State), | 
					
						
							|  |  |  |     .class_init    = m41t80_class_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void m41t80_register_types(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     type_register_static(&m41t80_info); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type_init(m41t80_register_types) |