misc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Miscellaneous Mac68K-specific stuff
  3. */
  4. #include <linux/types.h>
  5. #include <linux/errno.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/kernel.h>
  8. #include <linux/delay.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/time.h>
  12. #include <linux/rtc.h>
  13. #include <linux/mm.h>
  14. #include <linux/adb.h>
  15. #include <linux/cuda.h>
  16. #include <linux/pmu.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/io.h>
  19. #include <asm/rtc.h>
  20. #include <asm/system.h>
  21. #include <asm/segment.h>
  22. #include <asm/setup.h>
  23. #include <asm/macintosh.h>
  24. #include <asm/mac_via.h>
  25. #include <asm/mac_oss.h>
  26. #define BOOTINFO_COMPAT_1_0
  27. #include <asm/bootinfo.h>
  28. #include <asm/machdep.h>
  29. /* Offset between Unix time (1970-based) and Mac time (1904-based) */
  30. #define RTC_OFFSET 2082844800
  31. static void (*rom_reset)(void);
  32. #ifdef CONFIG_ADB_CUDA
  33. static long cuda_read_time(void)
  34. {
  35. struct adb_request req;
  36. long time;
  37. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
  38. return 0;
  39. while (!req.complete)
  40. cuda_poll();
  41. time = (req.reply[3] << 24) | (req.reply[4] << 16)
  42. | (req.reply[5] << 8) | req.reply[6];
  43. return time - RTC_OFFSET;
  44. }
  45. static void cuda_write_time(long data)
  46. {
  47. struct adb_request req;
  48. data += RTC_OFFSET;
  49. if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  50. (data >> 24) & 0xFF, (data >> 16) & 0xFF,
  51. (data >> 8) & 0xFF, data & 0xFF) < 0)
  52. return;
  53. while (!req.complete)
  54. cuda_poll();
  55. }
  56. static __u8 cuda_read_pram(int offset)
  57. {
  58. struct adb_request req;
  59. if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
  60. (offset >> 8) & 0xFF, offset & 0xFF) < 0)
  61. return 0;
  62. while (!req.complete)
  63. cuda_poll();
  64. return req.reply[3];
  65. }
  66. static void cuda_write_pram(int offset, __u8 data)
  67. {
  68. struct adb_request req;
  69. if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
  70. (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
  71. return;
  72. while (!req.complete)
  73. cuda_poll();
  74. }
  75. #else
  76. #define cuda_read_time() 0
  77. #define cuda_write_time(n)
  78. #define cuda_read_pram NULL
  79. #define cuda_write_pram NULL
  80. #endif
  81. #if 0 /* def CONFIG_ADB_PMU68K */
  82. static long pmu_read_time(void)
  83. {
  84. struct adb_request req;
  85. long time;
  86. if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  87. return 0;
  88. while (!req.complete)
  89. pmu_poll();
  90. time = (req.reply[0] << 24) | (req.reply[1] << 16)
  91. | (req.reply[2] << 8) | req.reply[3];
  92. return time - RTC_OFFSET;
  93. }
  94. static void pmu_write_time(long data)
  95. {
  96. struct adb_request req;
  97. data += RTC_OFFSET;
  98. if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
  99. (data >> 24) & 0xFF, (data >> 16) & 0xFF,
  100. (data >> 8) & 0xFF, data & 0xFF) < 0)
  101. return;
  102. while (!req.complete)
  103. pmu_poll();
  104. }
  105. static __u8 pmu_read_pram(int offset)
  106. {
  107. struct adb_request req;
  108. if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
  109. (offset >> 8) & 0xFF, offset & 0xFF) < 0)
  110. return 0;
  111. while (!req.complete)
  112. pmu_poll();
  113. return req.reply[3];
  114. }
  115. static void pmu_write_pram(int offset, __u8 data)
  116. {
  117. struct adb_request req;
  118. if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
  119. (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
  120. return;
  121. while (!req.complete)
  122. pmu_poll();
  123. }
  124. #else
  125. #define pmu_read_time() 0
  126. #define pmu_write_time(n)
  127. #define pmu_read_pram NULL
  128. #define pmu_write_pram NULL
  129. #endif
  130. #if 0 /* def CONFIG_ADB_MACIISI */
  131. extern int maciisi_request(struct adb_request *req,
  132. void (*done)(struct adb_request *), int nbytes, ...);
  133. static long maciisi_read_time(void)
  134. {
  135. struct adb_request req;
  136. long time;
  137. if (maciisi_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME))
  138. return 0;
  139. time = (req.reply[3] << 24) | (req.reply[4] << 16)
  140. | (req.reply[5] << 8) | req.reply[6];
  141. return time - RTC_OFFSET;
  142. }
  143. static void maciisi_write_time(long data)
  144. {
  145. struct adb_request req;
  146. data += RTC_OFFSET;
  147. maciisi_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  148. (data >> 24) & 0xFF, (data >> 16) & 0xFF,
  149. (data >> 8) & 0xFF, data & 0xFF);
  150. }
  151. static __u8 maciisi_read_pram(int offset)
  152. {
  153. struct adb_request req;
  154. if (maciisi_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
  155. (offset >> 8) & 0xFF, offset & 0xFF))
  156. return 0;
  157. return req.reply[3];
  158. }
  159. static void maciisi_write_pram(int offset, __u8 data)
  160. {
  161. struct adb_request req;
  162. maciisi_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
  163. (offset >> 8) & 0xFF, offset & 0xFF, data);
  164. }
  165. #else
  166. #define maciisi_read_time() 0
  167. #define maciisi_write_time(n)
  168. #define maciisi_read_pram NULL
  169. #define maciisi_write_pram NULL
  170. #endif
  171. /*
  172. * VIA PRAM/RTC access routines
  173. *
  174. * Must be called with interrupts disabled and
  175. * the RTC should be enabled.
  176. */
  177. static __u8 via_pram_readbyte(void)
  178. {
  179. int i,reg;
  180. __u8 data;
  181. reg = via1[vBufB] & ~VIA1B_vRTCClk;
  182. /* Set the RTC data line to be an input. */
  183. via1[vDirB] &= ~VIA1B_vRTCData;
  184. /* The bits of the byte come out in MSB order */
  185. data = 0;
  186. for (i = 0 ; i < 8 ; i++) {
  187. via1[vBufB] = reg;
  188. via1[vBufB] = reg | VIA1B_vRTCClk;
  189. data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
  190. }
  191. /* Return RTC data line to output state */
  192. via1[vDirB] |= VIA1B_vRTCData;
  193. return data;
  194. }
  195. static void via_pram_writebyte(__u8 data)
  196. {
  197. int i,reg,bit;
  198. reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
  199. /* The bits of the byte go in in MSB order */
  200. for (i = 0 ; i < 8 ; i++) {
  201. bit = data & 0x80? 1 : 0;
  202. data <<= 1;
  203. via1[vBufB] = reg | bit;
  204. via1[vBufB] = reg | bit | VIA1B_vRTCClk;
  205. }
  206. }
  207. /*
  208. * Execute a VIA PRAM/RTC command. For read commands
  209. * data should point to a one-byte buffer for the
  210. * resulting data. For write commands it should point
  211. * to the data byte to for the command.
  212. *
  213. * This function disables all interrupts while running.
  214. */
  215. static void via_pram_command(int command, __u8 *data)
  216. {
  217. unsigned long flags;
  218. int is_read;
  219. local_irq_save(flags);
  220. /* Enable the RTC and make sure the strobe line is high */
  221. via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
  222. if (command & 0xFF00) { /* extended (two-byte) command */
  223. via_pram_writebyte((command & 0xFF00) >> 8);
  224. via_pram_writebyte(command & 0xFF);
  225. is_read = command & 0x8000;
  226. } else { /* one-byte command */
  227. via_pram_writebyte(command);
  228. is_read = command & 0x80;
  229. }
  230. if (is_read) {
  231. *data = via_pram_readbyte();
  232. } else {
  233. via_pram_writebyte(*data);
  234. }
  235. /* All done, disable the RTC */
  236. via1[vBufB] |= VIA1B_vRTCEnb;
  237. local_irq_restore(flags);
  238. }
  239. static __u8 via_read_pram(int offset)
  240. {
  241. return 0;
  242. }
  243. static void via_write_pram(int offset, __u8 data)
  244. {
  245. }
  246. /*
  247. * Return the current time in seconds since January 1, 1904.
  248. *
  249. * This only works on machines with the VIA-based PRAM/RTC, which
  250. * is basically any machine with Mac II-style ADB.
  251. */
  252. static long via_read_time(void)
  253. {
  254. union {
  255. __u8 cdata[4];
  256. long idata;
  257. } result, last_result;
  258. int ct;
  259. /*
  260. * The NetBSD guys say to loop until you get the same reading
  261. * twice in a row.
  262. */
  263. ct = 0;
  264. do {
  265. if (++ct > 10) {
  266. printk("via_read_time: couldn't get valid time, "
  267. "last read = 0x%08lx and 0x%08lx\n",
  268. last_result.idata, result.idata);
  269. break;
  270. }
  271. last_result.idata = result.idata;
  272. result.idata = 0;
  273. via_pram_command(0x81, &result.cdata[3]);
  274. via_pram_command(0x85, &result.cdata[2]);
  275. via_pram_command(0x89, &result.cdata[1]);
  276. via_pram_command(0x8D, &result.cdata[0]);
  277. } while (result.idata != last_result.idata);
  278. return result.idata - RTC_OFFSET;
  279. }
  280. /*
  281. * Set the current time to a number of seconds since January 1, 1904.
  282. *
  283. * This only works on machines with the VIA-based PRAM/RTC, which
  284. * is basically any machine with Mac II-style ADB.
  285. */
  286. static void via_write_time(long time)
  287. {
  288. union {
  289. __u8 cdata[4];
  290. long idata;
  291. } data;
  292. __u8 temp;
  293. /* Clear the write protect bit */
  294. temp = 0x55;
  295. via_pram_command(0x35, &temp);
  296. data.idata = time + RTC_OFFSET;
  297. via_pram_command(0x01, &data.cdata[3]);
  298. via_pram_command(0x05, &data.cdata[2]);
  299. via_pram_command(0x09, &data.cdata[1]);
  300. via_pram_command(0x0D, &data.cdata[0]);
  301. /* Set the write protect bit */
  302. temp = 0xD5;
  303. via_pram_command(0x35, &temp);
  304. }
  305. static void via_shutdown(void)
  306. {
  307. if (rbv_present) {
  308. via2[rBufB] &= ~0x04;
  309. } else {
  310. /* Direction of vDirB is output */
  311. via2[vDirB] |= 0x04;
  312. /* Send a value of 0 on that line */
  313. via2[vBufB] &= ~0x04;
  314. mdelay(1000);
  315. }
  316. }
  317. /*
  318. * FIXME: not sure how this is supposed to work exactly...
  319. */
  320. static void oss_shutdown(void)
  321. {
  322. oss->rom_ctrl = OSS_POWEROFF;
  323. }
  324. #ifdef CONFIG_ADB_CUDA
  325. static void cuda_restart(void)
  326. {
  327. struct adb_request req;
  328. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_RESET_SYSTEM) < 0)
  329. return;
  330. while (!req.complete)
  331. cuda_poll();
  332. }
  333. static void cuda_shutdown(void)
  334. {
  335. struct adb_request req;
  336. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_POWERDOWN) < 0)
  337. return;
  338. while (!req.complete)
  339. cuda_poll();
  340. }
  341. #endif /* CONFIG_ADB_CUDA */
  342. #ifdef CONFIG_ADB_PMU68K
  343. void pmu_restart(void)
  344. {
  345. struct adb_request req;
  346. if (pmu_request(&req, NULL,
  347. 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
  348. return;
  349. while (!req.complete)
  350. pmu_poll();
  351. if (pmu_request(&req, NULL, 1, PMU_RESET) < 0)
  352. return;
  353. while (!req.complete)
  354. pmu_poll();
  355. }
  356. void pmu_shutdown(void)
  357. {
  358. struct adb_request req;
  359. if (pmu_request(&req, NULL,
  360. 2, PMU_SET_INTR_MASK, PMU_INT_ADB|PMU_INT_TICK) < 0)
  361. return;
  362. while (!req.complete)
  363. pmu_poll();
  364. if (pmu_request(&req, NULL, 5, PMU_SHUTDOWN, 'M', 'A', 'T', 'T') < 0)
  365. return;
  366. while (!req.complete)
  367. pmu_poll();
  368. }
  369. #endif
  370. /*
  371. *-------------------------------------------------------------------
  372. * Below this point are the generic routines; they'll dispatch to the
  373. * correct routine for the hardware on which we're running.
  374. *-------------------------------------------------------------------
  375. */
  376. void mac_pram_read(int offset, __u8 *buffer, int len)
  377. {
  378. __u8 (*func)(int);
  379. int i;
  380. switch(macintosh_config->adb_type) {
  381. case MAC_ADB_IISI:
  382. func = maciisi_read_pram; break;
  383. case MAC_ADB_PB1:
  384. case MAC_ADB_PB2:
  385. func = pmu_read_pram; break;
  386. case MAC_ADB_CUDA:
  387. func = cuda_read_pram; break;
  388. default:
  389. func = via_read_pram;
  390. }
  391. if (!func)
  392. return;
  393. for (i = 0 ; i < len ; i++) {
  394. buffer[i] = (*func)(offset++);
  395. }
  396. }
  397. void mac_pram_write(int offset, __u8 *buffer, int len)
  398. {
  399. void (*func)(int, __u8);
  400. int i;
  401. switch(macintosh_config->adb_type) {
  402. case MAC_ADB_IISI:
  403. func = maciisi_write_pram; break;
  404. case MAC_ADB_PB1:
  405. case MAC_ADB_PB2:
  406. func = pmu_write_pram; break;
  407. case MAC_ADB_CUDA:
  408. func = cuda_write_pram; break;
  409. default:
  410. func = via_write_pram;
  411. }
  412. if (!func)
  413. return;
  414. for (i = 0 ; i < len ; i++) {
  415. (*func)(offset++, buffer[i]);
  416. }
  417. }
  418. void mac_poweroff(void)
  419. {
  420. /*
  421. * MAC_ADB_IISI may need to be moved up here if it doesn't actually
  422. * work using the ADB packet method. --David Kilzer
  423. */
  424. if (oss_present) {
  425. oss_shutdown();
  426. } else if (macintosh_config->adb_type == MAC_ADB_II) {
  427. via_shutdown();
  428. #ifdef CONFIG_ADB_CUDA
  429. } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
  430. cuda_shutdown();
  431. #endif
  432. #ifdef CONFIG_ADB_PMU68K
  433. } else if (macintosh_config->adb_type == MAC_ADB_PB1
  434. || macintosh_config->adb_type == MAC_ADB_PB2) {
  435. pmu_shutdown();
  436. #endif
  437. }
  438. local_irq_enable();
  439. printk("It is now safe to turn off your Macintosh.\n");
  440. while(1);
  441. }
  442. void mac_reset(void)
  443. {
  444. if (macintosh_config->adb_type == MAC_ADB_II) {
  445. unsigned long flags;
  446. /* need ROMBASE in booter */
  447. /* indeed, plus need to MAP THE ROM !! */
  448. if (mac_bi_data.rombase == 0)
  449. mac_bi_data.rombase = 0x40800000;
  450. /* works on some */
  451. rom_reset = (void *) (mac_bi_data.rombase + 0xa);
  452. if (macintosh_config->ident == MAC_MODEL_SE30) {
  453. /*
  454. * MSch: Machines known to crash on ROM reset ...
  455. */
  456. } else {
  457. local_irq_save(flags);
  458. rom_reset();
  459. local_irq_restore(flags);
  460. }
  461. #ifdef CONFIG_ADB_CUDA
  462. } else if (macintosh_config->adb_type == MAC_ADB_CUDA) {
  463. cuda_restart();
  464. #endif
  465. #ifdef CONFIG_ADB_PMU68K
  466. } else if (macintosh_config->adb_type == MAC_ADB_PB1
  467. || macintosh_config->adb_type == MAC_ADB_PB2) {
  468. pmu_restart();
  469. #endif
  470. } else if (CPU_IS_030) {
  471. /* 030-specific reset routine. The idea is general, but the
  472. * specific registers to reset are '030-specific. Until I
  473. * have a non-030 machine, I can't test anything else.
  474. * -- C. Scott Ananian <cananian@alumni.princeton.edu>
  475. */
  476. unsigned long rombase = 0x40000000;
  477. /* make a 1-to-1 mapping, using the transparent tran. reg. */
  478. unsigned long virt = (unsigned long) mac_reset;
  479. unsigned long phys = virt_to_phys(mac_reset);
  480. unsigned long addr = (phys&0xFF000000)|0x8777;
  481. unsigned long offset = phys-virt;
  482. local_irq_disable(); /* lets not screw this up, ok? */
  483. __asm__ __volatile__(".chip 68030\n\t"
  484. "pmove %0,%/tt0\n\t"
  485. ".chip 68k"
  486. : : "m" (addr));
  487. /* Now jump to physical address so we can disable MMU */
  488. __asm__ __volatile__(
  489. ".chip 68030\n\t"
  490. "lea %/pc@(1f),%/a0\n\t"
  491. "addl %0,%/a0\n\t"/* fixup target address and stack ptr */
  492. "addl %0,%/sp\n\t"
  493. "pflusha\n\t"
  494. "jmp %/a0@\n\t" /* jump into physical memory */
  495. "0:.long 0\n\t" /* a constant zero. */
  496. /* OK. Now reset everything and jump to reset vector. */
  497. "1:\n\t"
  498. "lea %/pc@(0b),%/a0\n\t"
  499. "pmove %/a0@, %/tc\n\t" /* disable mmu */
  500. "pmove %/a0@, %/tt0\n\t" /* disable tt0 */
  501. "pmove %/a0@, %/tt1\n\t" /* disable tt1 */
  502. "movel #0, %/a0\n\t"
  503. "movec %/a0, %/vbr\n\t" /* clear vector base register */
  504. "movec %/a0, %/cacr\n\t" /* disable caches */
  505. "movel #0x0808,%/a0\n\t"
  506. "movec %/a0, %/cacr\n\t" /* flush i&d caches */
  507. "movew #0x2700,%/sr\n\t" /* set up status register */
  508. "movel %1@(0x0),%/a0\n\t"/* load interrupt stack pointer */
  509. "movec %/a0, %/isp\n\t"
  510. "movel %1@(0x4),%/a0\n\t" /* load reset vector */
  511. "reset\n\t" /* reset external devices */
  512. "jmp %/a0@\n\t" /* jump to the reset vector */
  513. ".chip 68k"
  514. : : "r" (offset), "a" (rombase) : "a0");
  515. }
  516. /* should never get here */
  517. local_irq_enable();
  518. printk ("Restart failed. Please restart manually.\n");
  519. while(1);
  520. }
  521. /*
  522. * This function translates seconds since 1970 into a proper date.
  523. *
  524. * Algorithm cribbed from glibc2.1, __offtime().
  525. */
  526. #define SECS_PER_MINUTE (60)
  527. #define SECS_PER_HOUR (SECS_PER_MINUTE * 60)
  528. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  529. static void unmktime(unsigned long time, long offset,
  530. int *yearp, int *monp, int *dayp,
  531. int *hourp, int *minp, int *secp)
  532. {
  533. /* How many days come before each month (0-12). */
  534. static const unsigned short int __mon_yday[2][13] =
  535. {
  536. /* Normal years. */
  537. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  538. /* Leap years. */
  539. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  540. };
  541. long int days, rem, y, wday, yday;
  542. const unsigned short int *ip;
  543. days = time / SECS_PER_DAY;
  544. rem = time % SECS_PER_DAY;
  545. rem += offset;
  546. while (rem < 0) {
  547. rem += SECS_PER_DAY;
  548. --days;
  549. }
  550. while (rem >= SECS_PER_DAY) {
  551. rem -= SECS_PER_DAY;
  552. ++days;
  553. }
  554. *hourp = rem / SECS_PER_HOUR;
  555. rem %= SECS_PER_HOUR;
  556. *minp = rem / SECS_PER_MINUTE;
  557. *secp = rem % SECS_PER_MINUTE;
  558. /* January 1, 1970 was a Thursday. */
  559. wday = (4 + days) % 7; /* Day in the week. Not currently used */
  560. if (wday < 0) wday += 7;
  561. y = 1970;
  562. #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
  563. #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
  564. #define __isleap(year) \
  565. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  566. while (days < 0 || days >= (__isleap (y) ? 366 : 365))
  567. {
  568. /* Guess a corrected year, assuming 365 days per year. */
  569. long int yg = y + days / 365 - (days % 365 < 0);
  570. /* Adjust DAYS and Y to match the guessed year. */
  571. days -= ((yg - y) * 365
  572. + LEAPS_THRU_END_OF (yg - 1)
  573. - LEAPS_THRU_END_OF (y - 1));
  574. y = yg;
  575. }
  576. *yearp = y - 1900;
  577. yday = days; /* day in the year. Not currently used. */
  578. ip = __mon_yday[__isleap(y)];
  579. for (y = 11; days < (long int) ip[y]; --y)
  580. continue;
  581. days -= ip[y];
  582. *monp = y;
  583. *dayp = days + 1; /* day in the month */
  584. return;
  585. }
  586. /*
  587. * Read/write the hardware clock.
  588. */
  589. int mac_hwclk(int op, struct rtc_time *t)
  590. {
  591. unsigned long now;
  592. if (!op) { /* read */
  593. switch (macintosh_config->adb_type) {
  594. case MAC_ADB_II:
  595. case MAC_ADB_IOP:
  596. now = via_read_time();
  597. break;
  598. case MAC_ADB_IISI:
  599. now = maciisi_read_time();
  600. break;
  601. case MAC_ADB_PB1:
  602. case MAC_ADB_PB2:
  603. now = pmu_read_time();
  604. break;
  605. case MAC_ADB_CUDA:
  606. now = cuda_read_time();
  607. break;
  608. default:
  609. now = 0;
  610. }
  611. t->tm_wday = 0;
  612. unmktime(now, 0,
  613. &t->tm_year, &t->tm_mon, &t->tm_mday,
  614. &t->tm_hour, &t->tm_min, &t->tm_sec);
  615. #if 0
  616. printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
  617. t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
  618. t->tm_hour, t->tm_min, t->tm_sec);
  619. #endif
  620. } else { /* write */
  621. #if 0
  622. printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
  623. t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
  624. t->tm_hour, t->tm_min, t->tm_sec);
  625. #endif
  626. now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
  627. t->tm_hour, t->tm_min, t->tm_sec);
  628. switch (macintosh_config->adb_type) {
  629. case MAC_ADB_II:
  630. case MAC_ADB_IOP:
  631. via_write_time(now);
  632. break;
  633. case MAC_ADB_CUDA:
  634. cuda_write_time(now);
  635. break;
  636. case MAC_ADB_PB1:
  637. case MAC_ADB_PB2:
  638. pmu_write_time(now);
  639. break;
  640. case MAC_ADB_IISI:
  641. maciisi_write_time(now);
  642. }
  643. }
  644. return 0;
  645. }
  646. /*
  647. * Set minutes/seconds in the hardware clock
  648. */
  649. int mac_set_clock_mmss (unsigned long nowtime)
  650. {
  651. struct rtc_time now;
  652. mac_hwclk(0, &now);
  653. now.tm_sec = nowtime % 60;
  654. now.tm_min = (nowtime / 60) % 60;
  655. mac_hwclk(1, &now);
  656. return 0;
  657. }