misc.c 17 KB

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