m48t59.c 19 KB

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