2
0

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 "qemu/bcd.h"
  36. #include "qemu/module.h"
  37. #include "m48t59-internal.h"
  38. #include "migration/vmstate.h"
  39. #define TYPE_M48TXX_SYS_BUS "sysbus-m48txx"
  40. #define M48TXX_SYS_BUS_GET_CLASS(obj) \
  41. OBJECT_GET_CLASS(M48txxSysBusDeviceClass, (obj), TYPE_M48TXX_SYS_BUS)
  42. #define M48TXX_SYS_BUS_CLASS(klass) \
  43. OBJECT_CLASS_CHECK(M48txxSysBusDeviceClass, (klass), TYPE_M48TXX_SYS_BUS)
  44. #define M48TXX_SYS_BUS(obj) \
  45. OBJECT_CHECK(M48txxSysBusState, (obj), TYPE_M48TXX_SYS_BUS)
  46. /*
  47. * Chipset docs:
  48. * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
  49. * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
  50. * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
  51. */
  52. typedef struct M48txxSysBusState {
  53. SysBusDevice parent_obj;
  54. M48t59State state;
  55. MemoryRegion io;
  56. } M48txxSysBusState;
  57. typedef struct M48txxSysBusDeviceClass {
  58. SysBusDeviceClass parent_class;
  59. M48txxInfo info;
  60. } M48txxSysBusDeviceClass;
  61. static M48txxInfo m48txx_sysbus_info[] = {
  62. {
  63. .bus_name = "sysbus-m48t02",
  64. .model = 2,
  65. .size = 0x800,
  66. },{
  67. .bus_name = "sysbus-m48t08",
  68. .model = 8,
  69. .size = 0x2000,
  70. },{
  71. .bus_name = "sysbus-m48t59",
  72. .model = 59,
  73. .size = 0x2000,
  74. }
  75. };
  76. /* Fake timer functions */
  77. /* Alarm management */
  78. static void alarm_cb (void *opaque)
  79. {
  80. struct tm tm;
  81. uint64_t next_time;
  82. M48t59State *NVRAM = opaque;
  83. qemu_set_irq(NVRAM->IRQ, 1);
  84. if ((NVRAM->buffer[0x1FF5] & 0x80) == 0 &&
  85. (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
  86. (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
  87. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  88. /* Repeat once a month */
  89. qemu_get_timedate(&tm, NVRAM->time_offset);
  90. tm.tm_mon++;
  91. if (tm.tm_mon == 13) {
  92. tm.tm_mon = 1;
  93. tm.tm_year++;
  94. }
  95. next_time = qemu_timedate_diff(&tm) - NVRAM->time_offset;
  96. } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
  97. (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
  98. (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
  99. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  100. /* Repeat once a day */
  101. next_time = 24 * 60 * 60;
  102. } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
  103. (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
  104. (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
  105. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  106. /* Repeat once an hour */
  107. next_time = 60 * 60;
  108. } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
  109. (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
  110. (NVRAM->buffer[0x1FF3] & 0x80) != 0 &&
  111. (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
  112. /* Repeat once a minute */
  113. next_time = 60;
  114. } else {
  115. /* Repeat once a second */
  116. next_time = 1;
  117. }
  118. timer_mod(NVRAM->alrm_timer, qemu_clock_get_ns(rtc_clock) +
  119. next_time * 1000);
  120. qemu_set_irq(NVRAM->IRQ, 0);
  121. }
  122. static void set_alarm(M48t59State *NVRAM)
  123. {
  124. int diff;
  125. if (NVRAM->alrm_timer != NULL) {
  126. timer_del(NVRAM->alrm_timer);
  127. diff = qemu_timedate_diff(&NVRAM->alarm) - NVRAM->time_offset;
  128. if (diff > 0)
  129. timer_mod(NVRAM->alrm_timer, diff * 1000);
  130. }
  131. }
  132. /* RTC management helpers */
  133. static inline void get_time(M48t59State *NVRAM, struct tm *tm)
  134. {
  135. qemu_get_timedate(tm, NVRAM->time_offset);
  136. }
  137. static void set_time(M48t59State *NVRAM, struct tm *tm)
  138. {
  139. NVRAM->time_offset = qemu_timedate_diff(tm);
  140. set_alarm(NVRAM);
  141. }
  142. /* Watchdog management */
  143. static void watchdog_cb (void *opaque)
  144. {
  145. M48t59State *NVRAM = opaque;
  146. NVRAM->buffer[0x1FF0] |= 0x80;
  147. if (NVRAM->buffer[0x1FF7] & 0x80) {
  148. NVRAM->buffer[0x1FF7] = 0x00;
  149. NVRAM->buffer[0x1FFC] &= ~0x40;
  150. /* May it be a hw CPU Reset instead ? */
  151. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  152. } else {
  153. qemu_set_irq(NVRAM->IRQ, 1);
  154. qemu_set_irq(NVRAM->IRQ, 0);
  155. }
  156. }
  157. static void set_up_watchdog(M48t59State *NVRAM, uint8_t value)
  158. {
  159. uint64_t interval; /* in 1/16 seconds */
  160. NVRAM->buffer[0x1FF0] &= ~0x80;
  161. if (NVRAM->wd_timer != NULL) {
  162. timer_del(NVRAM->wd_timer);
  163. if (value != 0) {
  164. interval = (1 << (2 * (value & 0x03))) * ((value >> 2) & 0x1F);
  165. timer_mod(NVRAM->wd_timer, ((uint64_t)time(NULL) * 1000) +
  166. ((interval * 1000) >> 4));
  167. }
  168. }
  169. }
  170. /* Direct access to NVRAM */
  171. void m48t59_write(M48t59State *NVRAM, uint32_t addr, uint32_t val)
  172. {
  173. struct tm tm;
  174. int tmp;
  175. if (addr > 0x1FF8 && addr < 0x2000)
  176. NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, 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. if (addr > 0x1FF9 && addr < 0x2000)
  429. NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__, 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. NVRAM_PRINTF("%s: 0x%"HWADDR_PRIx" => 0x%"PRIx64"\n", __func__, 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. NVRAM_PRINTF("%s: 0x%"HWADDR_PRIx" <= 0x%08x\n", __func__, 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_create(NULL, m48txx_sysbus_info[i].bus_name);
  539. qdev_prop_set_int32(dev, "base-year", base_year);
  540. qdev_init_nofail(dev);
  541. s = SYS_BUS_DEVICE(dev);
  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. dc->props = 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)