m48t59.c 21 KB

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