misc.c 17 KB

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