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