misc.c 17 KB

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