m48t59.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
  3. *
  4. * Copyright (c) 2003-2005, 2007, 2017 Jocelyn Mayer
  5. * Copyright (c) 2013 Hervé Poussineau
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu-common.h"
  27. #include "hw/irq.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/rtc/m48t59.h"
  30. #include "qemu/timer.h"
  31. #include "sysemu/runstate.h"
  32. #include "sysemu/sysemu.h"
  33. #include "hw/sysbus.h"
  34. #include "exec/address-spaces.h"
  35. #include "qapi/error.h"
  36. #include "qemu/bcd.h"
  37. #include "qemu/module.h"
  38. #include "trace.h"
  39. #include "m48t59-internal.h"
  40. #include "migration/vmstate.h"
  41. #include "qom/object.h"
  42. #define TYPE_M48TXX_SYS_BUS "sysbus-m48txx"
  43. typedef struct M48txxSysBusDeviceClass M48txxSysBusDeviceClass;
  44. typedef struct M48txxSysBusState M48txxSysBusState;
  45. DECLARE_OBJ_CHECKERS(M48txxSysBusState, M48txxSysBusDeviceClass,
  46. M48TXX_SYS_BUS, TYPE_M48TXX_SYS_BUS)
  47. /*
  48. * Chipset docs:
  49. * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
  50. * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
  51. * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
  52. */
  53. struct M48txxSysBusState {
  54. SysBusDevice parent_obj;
  55. M48t59State state;
  56. MemoryRegion io;
  57. };
  58. struct M48txxSysBusDeviceClass {
  59. SysBusDeviceClass parent_class;
  60. M48txxInfo info;
  61. };
  62. static M48txxInfo m48txx_sysbus_info[] = {
  63. {
  64. .bus_name = "sysbus-m48t02",
  65. .model = 2,
  66. .size = 0x800,
  67. },{
  68. .bus_name = "sysbus-m48t08",
  69. .model = 8,
  70. .size = 0x2000,
  71. },{
  72. .bus_name = "sysbus-m48t59",
  73. .model = 59,
  74. .size = 0x2000,
  75. }
  76. };
  77. /* Fake timer functions */
  78. /* Alarm management */
  79. static void alarm_cb (void *opaque)
  80. {
  81. struct tm tm;
  82. uint64_t next_time;
  83. M48t59State *NVRAM = opaque;
  84. qemu_set_irq(NVRAM->IRQ, 1);
  85. if ((NVRAM->buffer[0x1FF5] & 0x80) == 0 &&
  86. (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
  87. (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
  88. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  89. /* Repeat once a month */
  90. qemu_get_timedate(&tm, NVRAM->time_offset);
  91. tm.tm_mon++;
  92. if (tm.tm_mon == 13) {
  93. tm.tm_mon = 1;
  94. tm.tm_year++;
  95. }
  96. next_time = qemu_timedate_diff(&tm) - NVRAM->time_offset;
  97. } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
  98. (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
  99. (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
  100. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  101. /* Repeat once a day */
  102. next_time = 24 * 60 * 60;
  103. } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
  104. (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
  105. (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
  106. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  107. /* Repeat once an hour */
  108. next_time = 60 * 60;
  109. } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
  110. (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
  111. (NVRAM->buffer[0x1FF3] & 0x80) != 0 &&
  112. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  113. /* Repeat once a minute */
  114. next_time = 60;
  115. } else {
  116. /* Repeat once a second */
  117. next_time = 1;
  118. }
  119. timer_mod(NVRAM->alrm_timer, qemu_clock_get_ns(rtc_clock) +
  120. next_time * 1000);
  121. qemu_set_irq(NVRAM->IRQ, 0);
  122. }
  123. static void set_alarm(M48t59State *NVRAM)
  124. {
  125. int diff;
  126. if (NVRAM->alrm_timer != NULL) {
  127. timer_del(NVRAM->alrm_timer);
  128. diff = qemu_timedate_diff(&NVRAM->alarm) - NVRAM->time_offset;
  129. if (diff > 0)
  130. timer_mod(NVRAM->alrm_timer, diff * 1000);
  131. }
  132. }
  133. /* RTC management helpers */
  134. static inline void get_time(M48t59State *NVRAM, struct tm *tm)
  135. {
  136. qemu_get_timedate(tm, NVRAM->time_offset);
  137. }
  138. static void set_time(M48t59State *NVRAM, struct tm *tm)
  139. {
  140. NVRAM->time_offset = qemu_timedate_diff(tm);
  141. set_alarm(NVRAM);
  142. }
  143. /* Watchdog management */
  144. static void watchdog_cb (void *opaque)
  145. {
  146. M48t59State *NVRAM = opaque;
  147. NVRAM->buffer[0x1FF0] |= 0x80;
  148. if (NVRAM->buffer[0x1FF7] & 0x80) {
  149. NVRAM->buffer[0x1FF7] = 0x00;
  150. NVRAM->buffer[0x1FFC] &= ~0x40;
  151. /* May it be a hw CPU Reset instead ? */
  152. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  153. } else {
  154. qemu_set_irq(NVRAM->IRQ, 1);
  155. qemu_set_irq(NVRAM->IRQ, 0);
  156. }
  157. }
  158. static void set_up_watchdog(M48t59State *NVRAM, uint8_t value)
  159. {
  160. uint64_t interval; /* in 1/16 seconds */
  161. NVRAM->buffer[0x1FF0] &= ~0x80;
  162. if (NVRAM->wd_timer != NULL) {
  163. timer_del(NVRAM->wd_timer);
  164. if (value != 0) {
  165. interval = (1 << (2 * (value & 0x03))) * ((value >> 2) & 0x1F);
  166. timer_mod(NVRAM->wd_timer, ((uint64_t)time(NULL) * 1000) +
  167. ((interval * 1000) >> 4));
  168. }
  169. }
  170. }
  171. /* Direct access to NVRAM */
  172. void m48t59_write(M48t59State *NVRAM, uint32_t addr, uint32_t val)
  173. {
  174. struct tm tm;
  175. int tmp;
  176. trace_m48txx_nvram_mem_write(addr, val);
  177. /* check for NVRAM access */
  178. if ((NVRAM->model == 2 && addr < 0x7f8) ||
  179. (NVRAM->model == 8 && addr < 0x1ff8) ||
  180. (NVRAM->model == 59 && addr < 0x1ff0)) {
  181. goto do_write;
  182. }
  183. /* TOD access */
  184. switch (addr) {
  185. case 0x1FF0:
  186. /* flags register : read-only */
  187. break;
  188. case 0x1FF1:
  189. /* unused */
  190. break;
  191. case 0x1FF2:
  192. /* alarm seconds */
  193. tmp = from_bcd(val & 0x7F);
  194. if (tmp >= 0 && tmp <= 59) {
  195. NVRAM->alarm.tm_sec = tmp;
  196. NVRAM->buffer[0x1FF2] = val;
  197. set_alarm(NVRAM);
  198. }
  199. break;
  200. case 0x1FF3:
  201. /* alarm minutes */
  202. tmp = from_bcd(val & 0x7F);
  203. if (tmp >= 0 && tmp <= 59) {
  204. NVRAM->alarm.tm_min = tmp;
  205. NVRAM->buffer[0x1FF3] = val;
  206. set_alarm(NVRAM);
  207. }
  208. break;
  209. case 0x1FF4:
  210. /* alarm hours */
  211. tmp = from_bcd(val & 0x3F);
  212. if (tmp >= 0 && tmp <= 23) {
  213. NVRAM->alarm.tm_hour = tmp;
  214. NVRAM->buffer[0x1FF4] = val;
  215. set_alarm(NVRAM);
  216. }
  217. break;
  218. case 0x1FF5:
  219. /* alarm date */
  220. tmp = from_bcd(val & 0x3F);
  221. if (tmp != 0) {
  222. NVRAM->alarm.tm_mday = tmp;
  223. NVRAM->buffer[0x1FF5] = val;
  224. set_alarm(NVRAM);
  225. }
  226. break;
  227. case 0x1FF6:
  228. /* interrupts */
  229. NVRAM->buffer[0x1FF6] = val;
  230. break;
  231. case 0x1FF7:
  232. /* watchdog */
  233. NVRAM->buffer[0x1FF7] = val;
  234. set_up_watchdog(NVRAM, val);
  235. break;
  236. case 0x1FF8:
  237. case 0x07F8:
  238. /* control */
  239. NVRAM->buffer[addr] = (val & ~0xA0) | 0x90;
  240. break;
  241. case 0x1FF9:
  242. case 0x07F9:
  243. /* seconds (BCD) */
  244. tmp = from_bcd(val & 0x7F);
  245. if (tmp >= 0 && tmp <= 59) {
  246. get_time(NVRAM, &tm);
  247. tm.tm_sec = tmp;
  248. set_time(NVRAM, &tm);
  249. }
  250. if ((val & 0x80) ^ (NVRAM->buffer[addr] & 0x80)) {
  251. if (val & 0x80) {
  252. NVRAM->stop_time = time(NULL);
  253. } else {
  254. NVRAM->time_offset += NVRAM->stop_time - time(NULL);
  255. NVRAM->stop_time = 0;
  256. }
  257. }
  258. NVRAM->buffer[addr] = val & 0x80;
  259. break;
  260. case 0x1FFA:
  261. case 0x07FA:
  262. /* minutes (BCD) */
  263. tmp = from_bcd(val & 0x7F);
  264. if (tmp >= 0 && tmp <= 59) {
  265. get_time(NVRAM, &tm);
  266. tm.tm_min = tmp;
  267. set_time(NVRAM, &tm);
  268. }
  269. break;
  270. case 0x1FFB:
  271. case 0x07FB:
  272. /* hours (BCD) */
  273. tmp = from_bcd(val & 0x3F);
  274. if (tmp >= 0 && tmp <= 23) {
  275. get_time(NVRAM, &tm);
  276. tm.tm_hour = tmp;
  277. set_time(NVRAM, &tm);
  278. }
  279. break;
  280. case 0x1FFC:
  281. case 0x07FC:
  282. /* day of the week / century */
  283. tmp = from_bcd(val & 0x07);
  284. get_time(NVRAM, &tm);
  285. tm.tm_wday = tmp;
  286. set_time(NVRAM, &tm);
  287. NVRAM->buffer[addr] = val & 0x40;
  288. break;
  289. case 0x1FFD:
  290. case 0x07FD:
  291. /* date (BCD) */
  292. tmp = from_bcd(val & 0x3F);
  293. if (tmp != 0) {
  294. get_time(NVRAM, &tm);
  295. tm.tm_mday = tmp;
  296. set_time(NVRAM, &tm);
  297. }
  298. break;
  299. case 0x1FFE:
  300. case 0x07FE:
  301. /* month */
  302. tmp = from_bcd(val & 0x1F);
  303. if (tmp >= 1 && tmp <= 12) {
  304. get_time(NVRAM, &tm);
  305. tm.tm_mon = tmp - 1;
  306. set_time(NVRAM, &tm);
  307. }
  308. break;
  309. case 0x1FFF:
  310. case 0x07FF:
  311. /* year */
  312. tmp = from_bcd(val);
  313. if (tmp >= 0 && tmp <= 99) {
  314. get_time(NVRAM, &tm);
  315. tm.tm_year = from_bcd(val) + NVRAM->base_year - 1900;
  316. set_time(NVRAM, &tm);
  317. }
  318. break;
  319. default:
  320. /* Check lock registers state */
  321. if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
  322. break;
  323. if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
  324. break;
  325. do_write:
  326. if (addr < NVRAM->size) {
  327. NVRAM->buffer[addr] = val & 0xFF;
  328. }
  329. break;
  330. }
  331. }
  332. uint32_t m48t59_read(M48t59State *NVRAM, uint32_t addr)
  333. {
  334. struct tm tm;
  335. uint32_t retval = 0xFF;
  336. /* check for NVRAM access */
  337. if ((NVRAM->model == 2 && addr < 0x078f) ||
  338. (NVRAM->model == 8 && addr < 0x1ff8) ||
  339. (NVRAM->model == 59 && addr < 0x1ff0)) {
  340. goto do_read;
  341. }
  342. /* TOD access */
  343. switch (addr) {
  344. case 0x1FF0:
  345. /* flags register */
  346. goto do_read;
  347. case 0x1FF1:
  348. /* unused */
  349. retval = 0;
  350. break;
  351. case 0x1FF2:
  352. /* alarm seconds */
  353. goto do_read;
  354. case 0x1FF3:
  355. /* alarm minutes */
  356. goto do_read;
  357. case 0x1FF4:
  358. /* alarm hours */
  359. goto do_read;
  360. case 0x1FF5:
  361. /* alarm date */
  362. goto do_read;
  363. case 0x1FF6:
  364. /* interrupts */
  365. goto do_read;
  366. case 0x1FF7:
  367. /* A read resets the watchdog */
  368. set_up_watchdog(NVRAM, NVRAM->buffer[0x1FF7]);
  369. goto do_read;
  370. case 0x1FF8:
  371. case 0x07F8:
  372. /* control */
  373. goto do_read;
  374. case 0x1FF9:
  375. case 0x07F9:
  376. /* seconds (BCD) */
  377. get_time(NVRAM, &tm);
  378. retval = (NVRAM->buffer[addr] & 0x80) | to_bcd(tm.tm_sec);
  379. break;
  380. case 0x1FFA:
  381. case 0x07FA:
  382. /* minutes (BCD) */
  383. get_time(NVRAM, &tm);
  384. retval = to_bcd(tm.tm_min);
  385. break;
  386. case 0x1FFB:
  387. case 0x07FB:
  388. /* hours (BCD) */
  389. get_time(NVRAM, &tm);
  390. retval = to_bcd(tm.tm_hour);
  391. break;
  392. case 0x1FFC:
  393. case 0x07FC:
  394. /* day of the week / century */
  395. get_time(NVRAM, &tm);
  396. retval = NVRAM->buffer[addr] | tm.tm_wday;
  397. break;
  398. case 0x1FFD:
  399. case 0x07FD:
  400. /* date */
  401. get_time(NVRAM, &tm);
  402. retval = to_bcd(tm.tm_mday);
  403. break;
  404. case 0x1FFE:
  405. case 0x07FE:
  406. /* month */
  407. get_time(NVRAM, &tm);
  408. retval = to_bcd(tm.tm_mon + 1);
  409. break;
  410. case 0x1FFF:
  411. case 0x07FF:
  412. /* year */
  413. get_time(NVRAM, &tm);
  414. retval = to_bcd((tm.tm_year + 1900 - NVRAM->base_year) % 100);
  415. break;
  416. default:
  417. /* Check lock registers state */
  418. if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
  419. break;
  420. if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
  421. break;
  422. do_read:
  423. if (addr < NVRAM->size) {
  424. retval = NVRAM->buffer[addr];
  425. }
  426. break;
  427. }
  428. trace_m48txx_nvram_mem_read(addr, retval);
  429. return retval;
  430. }
  431. /* IO access to NVRAM */
  432. static void NVRAM_writeb(void *opaque, hwaddr addr, uint64_t val,
  433. unsigned size)
  434. {
  435. M48t59State *NVRAM = opaque;
  436. trace_m48txx_nvram_io_write(addr, val);
  437. switch (addr) {
  438. case 0:
  439. NVRAM->addr &= ~0x00FF;
  440. NVRAM->addr |= val;
  441. break;
  442. case 1:
  443. NVRAM->addr &= ~0xFF00;
  444. NVRAM->addr |= val << 8;
  445. break;
  446. case 3:
  447. m48t59_write(NVRAM, NVRAM->addr, val);
  448. NVRAM->addr = 0x0000;
  449. break;
  450. default:
  451. break;
  452. }
  453. }
  454. static uint64_t NVRAM_readb(void *opaque, hwaddr addr, unsigned size)
  455. {
  456. M48t59State *NVRAM = opaque;
  457. uint32_t retval;
  458. switch (addr) {
  459. case 3:
  460. retval = m48t59_read(NVRAM, NVRAM->addr);
  461. break;
  462. default:
  463. retval = -1;
  464. break;
  465. }
  466. trace_m48txx_nvram_io_read(addr, retval);
  467. return retval;
  468. }
  469. static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
  470. {
  471. M48t59State *NVRAM = opaque;
  472. return m48t59_read(NVRAM, addr);
  473. }
  474. static void nvram_write(void *opaque, hwaddr addr, uint64_t value,
  475. unsigned size)
  476. {
  477. M48t59State *NVRAM = opaque;
  478. return m48t59_write(NVRAM, addr, value);
  479. }
  480. static const MemoryRegionOps nvram_ops = {
  481. .read = nvram_read,
  482. .write = nvram_write,
  483. .impl.min_access_size = 1,
  484. .impl.max_access_size = 1,
  485. .valid.min_access_size = 1,
  486. .valid.max_access_size = 4,
  487. .endianness = DEVICE_BIG_ENDIAN,
  488. };
  489. static const VMStateDescription vmstate_m48t59 = {
  490. .name = "m48t59",
  491. .version_id = 1,
  492. .minimum_version_id = 1,
  493. .fields = (VMStateField[]) {
  494. VMSTATE_UINT8(lock, M48t59State),
  495. VMSTATE_UINT16(addr, M48t59State),
  496. VMSTATE_VBUFFER_UINT32(buffer, M48t59State, 0, NULL, size),
  497. VMSTATE_END_OF_LIST()
  498. }
  499. };
  500. void m48t59_reset_common(M48t59State *NVRAM)
  501. {
  502. NVRAM->addr = 0;
  503. NVRAM->lock = 0;
  504. if (NVRAM->alrm_timer != NULL)
  505. timer_del(NVRAM->alrm_timer);
  506. if (NVRAM->wd_timer != NULL)
  507. timer_del(NVRAM->wd_timer);
  508. }
  509. static void m48t59_reset_sysbus(DeviceState *d)
  510. {
  511. M48txxSysBusState *sys = M48TXX_SYS_BUS(d);
  512. M48t59State *NVRAM = &sys->state;
  513. m48t59_reset_common(NVRAM);
  514. }
  515. const MemoryRegionOps m48t59_io_ops = {
  516. .read = NVRAM_readb,
  517. .write = NVRAM_writeb,
  518. .impl = {
  519. .min_access_size = 1,
  520. .max_access_size = 1,
  521. },
  522. .endianness = DEVICE_LITTLE_ENDIAN,
  523. };
  524. /* Initialisation routine */
  525. Nvram *m48t59_init(qemu_irq IRQ, hwaddr mem_base,
  526. uint32_t io_base, uint16_t size, int base_year,
  527. int model)
  528. {
  529. DeviceState *dev;
  530. SysBusDevice *s;
  531. int i;
  532. for (i = 0; i < ARRAY_SIZE(m48txx_sysbus_info); i++) {
  533. if (m48txx_sysbus_info[i].size != size ||
  534. m48txx_sysbus_info[i].model != model) {
  535. continue;
  536. }
  537. dev = qdev_new(m48txx_sysbus_info[i].bus_name);
  538. qdev_prop_set_int32(dev, "base-year", base_year);
  539. s = SYS_BUS_DEVICE(dev);
  540. sysbus_realize_and_unref(s, &error_fatal);
  541. sysbus_connect_irq(s, 0, IRQ);
  542. if (io_base != 0) {
  543. memory_region_add_subregion(get_system_io(), io_base,
  544. sysbus_mmio_get_region(s, 1));
  545. }
  546. if (mem_base != 0) {
  547. sysbus_mmio_map(s, 0, mem_base);
  548. }
  549. return NVRAM(s);
  550. }
  551. assert(false);
  552. return NULL;
  553. }
  554. void m48t59_realize_common(M48t59State *s, Error **errp)
  555. {
  556. s->buffer = g_malloc0(s->size);
  557. if (s->model == 59) {
  558. s->alrm_timer = timer_new_ns(rtc_clock, &alarm_cb, s);
  559. s->wd_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &watchdog_cb, s);
  560. }
  561. qemu_get_timedate(&s->alarm, 0);
  562. }
  563. static void m48t59_init1(Object *obj)
  564. {
  565. M48txxSysBusDeviceClass *u = M48TXX_SYS_BUS_GET_CLASS(obj);
  566. M48txxSysBusState *d = M48TXX_SYS_BUS(obj);
  567. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  568. M48t59State *s = &d->state;
  569. s->model = u->info.model;
  570. s->size = u->info.size;
  571. sysbus_init_irq(dev, &s->IRQ);
  572. memory_region_init_io(&s->iomem, obj, &nvram_ops, s, "m48t59.nvram",
  573. s->size);
  574. memory_region_init_io(&d->io, obj, &m48t59_io_ops, s, "m48t59", 4);
  575. }
  576. static void m48t59_realize(DeviceState *dev, Error **errp)
  577. {
  578. M48txxSysBusState *d = M48TXX_SYS_BUS(dev);
  579. M48t59State *s = &d->state;
  580. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  581. sysbus_init_mmio(sbd, &s->iomem);
  582. sysbus_init_mmio(sbd, &d->io);
  583. m48t59_realize_common(s, errp);
  584. }
  585. static uint32_t m48txx_sysbus_read(Nvram *obj, uint32_t addr)
  586. {
  587. M48txxSysBusState *d = M48TXX_SYS_BUS(obj);
  588. return m48t59_read(&d->state, addr);
  589. }
  590. static void m48txx_sysbus_write(Nvram *obj, uint32_t addr, uint32_t val)
  591. {
  592. M48txxSysBusState *d = M48TXX_SYS_BUS(obj);
  593. m48t59_write(&d->state, addr, val);
  594. }
  595. static void m48txx_sysbus_toggle_lock(Nvram *obj, int lock)
  596. {
  597. M48txxSysBusState *d = M48TXX_SYS_BUS(obj);
  598. m48t59_toggle_lock(&d->state, lock);
  599. }
  600. static Property m48t59_sysbus_properties[] = {
  601. DEFINE_PROP_INT32("base-year", M48txxSysBusState, state.base_year, 0),
  602. DEFINE_PROP_END_OF_LIST(),
  603. };
  604. static void m48txx_sysbus_class_init(ObjectClass *klass, void *data)
  605. {
  606. DeviceClass *dc = DEVICE_CLASS(klass);
  607. NvramClass *nc = NVRAM_CLASS(klass);
  608. dc->realize = m48t59_realize;
  609. dc->reset = m48t59_reset_sysbus;
  610. device_class_set_props(dc, m48t59_sysbus_properties);
  611. dc->vmsd = &vmstate_m48t59;
  612. nc->read = m48txx_sysbus_read;
  613. nc->write = m48txx_sysbus_write;
  614. nc->toggle_lock = m48txx_sysbus_toggle_lock;
  615. }
  616. static void m48txx_sysbus_concrete_class_init(ObjectClass *klass, void *data)
  617. {
  618. M48txxSysBusDeviceClass *u = M48TXX_SYS_BUS_CLASS(klass);
  619. M48txxInfo *info = data;
  620. u->info = *info;
  621. }
  622. static const TypeInfo nvram_info = {
  623. .name = TYPE_NVRAM,
  624. .parent = TYPE_INTERFACE,
  625. .class_size = sizeof(NvramClass),
  626. };
  627. static const TypeInfo m48txx_sysbus_type_info = {
  628. .name = TYPE_M48TXX_SYS_BUS,
  629. .parent = TYPE_SYS_BUS_DEVICE,
  630. .instance_size = sizeof(M48txxSysBusState),
  631. .instance_init = m48t59_init1,
  632. .abstract = true,
  633. .class_init = m48txx_sysbus_class_init,
  634. .interfaces = (InterfaceInfo[]) {
  635. { TYPE_NVRAM },
  636. { }
  637. }
  638. };
  639. static void m48t59_register_types(void)
  640. {
  641. TypeInfo sysbus_type_info = {
  642. .parent = TYPE_M48TXX_SYS_BUS,
  643. .class_size = sizeof(M48txxSysBusDeviceClass),
  644. .class_init = m48txx_sysbus_concrete_class_init,
  645. };
  646. int i;
  647. type_register_static(&nvram_info);
  648. type_register_static(&m48txx_sysbus_type_info);
  649. for (i = 0; i < ARRAY_SIZE(m48txx_sysbus_info); i++) {
  650. sysbus_type_info.name = m48txx_sysbus_info[i].bus_name;
  651. sysbus_type_info.class_data = &m48txx_sysbus_info[i];
  652. type_register(&sysbus_type_info);
  653. }
  654. }
  655. type_init(m48t59_register_types)