misc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Miscellaneous Mac68K-specific stuff
  3. */
  4. #include <linux/types.h>
  5. #include <linux/errno.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/kernel.h>
  8. #include <linux/delay.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/time.h>
  12. #include <linux/rtc.h>
  13. #include <linux/mm.h>
  14. #include <linux/adb.h>
  15. #include <linux/cuda.h>
  16. #include <linux/pmu.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/io.h>
  19. #include <asm/rtc.h>
  20. #include <asm/system.h>
  21. #include <asm/segment.h>
  22. #include <asm/setup.h>
  23. #include <asm/macintosh.h>
  24. #include <asm/mac_via.h>
  25. #include <asm/mac_oss.h>
  26. #define BOOTINFO_COMPAT_1_0
  27. #include <asm/bootinfo.h>
  28. #include <asm/machdep.h>
  29. /* Offset between Unix time (1970-based) and Mac time (1904-based) */
  30. #define RTC_OFFSET 2082844800
  31. extern struct mac_booter_data mac_bi_data;
  32. static void (*rom_reset)(void);
  33. #ifdef CONFIG_ADB_CUDA
  34. static long cuda_read_time(void)
  35. {
  36. struct adb_request req;
  37. long time;
  38. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
  39. return 0;
  40. while (!req.complete)
  41. cuda_poll();
  42. time = (req.reply[3] << 24) | (req.reply[4] << 16)
  43. | (req.reply[5] << 8) | req.reply[6];
  44. return time - RTC_OFFSET;
  45. }
  46. static void cuda_write_time(long data)
  47. {
  48. struct adb_request req;
  49. data += RTC_OFFSET;
  50. if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  51. (data >> 24) & 0xFF, (data >> 16) & 0xFF,
  52. (data >> 8) & 0xFF, data & 0xFF) < 0)
  53. return;
  54. while (!req.complete)
  55. cuda_poll();
  56. }
  57. static __u8 cuda_read_pram(int offset)
  58. {
  59. struct adb_request req;
  60. if (cuda_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
  61. (offset >> 8) & 0xFF, offset & 0xFF) < 0)
  62. return 0;
  63. while (!req.complete)
  64. cuda_poll();
  65. return req.reply[3];
  66. }
  67. static void cuda_write_pram(int offset, __u8 data)
  68. {
  69. struct adb_request req;
  70. if (cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
  71. (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
  72. return;
  73. while (!req.complete)
  74. cuda_poll();
  75. }
  76. #else
  77. #define cuda_read_time() 0
  78. #define cuda_write_time(n)
  79. #define cuda_read_pram NULL
  80. #define cuda_write_pram NULL
  81. #endif
  82. #ifdef CONFIG_ADB_PMU68K
  83. static long pmu_read_time(void)
  84. {
  85. struct adb_request req;
  86. long time;
  87. if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  88. return 0;
  89. while (!req.complete)
  90. pmu_poll();
  91. time = (req.reply[0] << 24) | (req.reply[1] << 16)
  92. | (req.reply[2] << 8) | req.reply[3];
  93. return time - RTC_OFFSET;
  94. }
  95. static void pmu_write_time(long data)
  96. {
  97. struct adb_request req;
  98. data += RTC_OFFSET;
  99. if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
  100. (data >> 24) & 0xFF, (data >> 16) & 0xFF,
  101. (data >> 8) & 0xFF, data & 0xFF) < 0)
  102. return;
  103. while (!req.complete)
  104. pmu_poll();
  105. }
  106. static __u8 pmu_read_pram(int offset)
  107. {
  108. struct adb_request req;
  109. if (pmu_request(&req, NULL, 3, PMU_READ_NVRAM,
  110. (offset >> 8) & 0xFF, offset & 0xFF) < 0)
  111. return 0;
  112. while (!req.complete)
  113. pmu_poll();
  114. return req.reply[3];
  115. }
  116. static void pmu_write_pram(int offset, __u8 data)
  117. {
  118. struct adb_request req;
  119. if (pmu_request(&req, NULL, 4, PMU_WRITE_NVRAM,
  120. (offset >> 8) & 0xFF, offset & 0xFF, data) < 0)
  121. return;
  122. while (!req.complete)
  123. pmu_poll();
  124. }
  125. #else
  126. #define pmu_read_time() 0
  127. #define pmu_write_time(n)
  128. #define pmu_read_pram NULL
  129. #define pmu_write_pram NULL
  130. #endif
  131. #ifdef CONFIG_ADB_MACIISI
  132. extern int maciisi_request(struct adb_request *req,
  133. void (*done)(struct adb_request *), int nbytes, ...);
  134. static long maciisi_read_time(void)
  135. {
  136. struct adb_request req;
  137. long time;
  138. if (maciisi_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME))
  139. return 0;
  140. time = (req.reply[3] << 24) | (req.reply[4] << 16)
  141. | (req.reply[5] << 8) | req.reply[6];
  142. return time - RTC_OFFSET;
  143. }
  144. static void maciisi_write_time(long data)
  145. {
  146. struct adb_request req;
  147. data += RTC_OFFSET;
  148. maciisi_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  149. (data >> 24) & 0xFF, (data >> 16) & 0xFF,
  150. (data >> 8) & 0xFF, data & 0xFF);
  151. }
  152. static __u8 maciisi_read_pram(int offset)
  153. {
  154. struct adb_request req;
  155. if (maciisi_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM,
  156. (offset >> 8) & 0xFF, offset & 0xFF))
  157. return 0;
  158. return req.reply[3];
  159. }
  160. static void maciisi_write_pram(int offset, __u8 data)
  161. {
  162. struct adb_request req;
  163. maciisi_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM,
  164. (offset >> 8) & 0xFF, offset & 0xFF, data);
  165. }
  166. #else
  167. #define maciisi_read_time() 0
  168. #define maciisi_write_time(n)
  169. #define maciisi_read_pram NULL
  170. #define maciisi_write_pram NULL
  171. #endif
  172. /*
  173. * VIA PRAM/RTC access routines
  174. *
  175. * Must be called with interrupts disabled and
  176. * the RTC should be enabled.
  177. */
  178. static __u8 via_pram_readbyte(void)
  179. {
  180. int i,reg;
  181. __u8 data;
  182. reg = via1[vBufB] & ~VIA1B_vRTCClk;
  183. /* Set the RTC data line to be an input. */
  184. via1[vDirB] &= ~VIA1B_vRTCData;
  185. /* The bits of the byte come out in MSB order */
  186. data = 0;
  187. for (i = 0 ; i < 8 ; i++) {
  188. via1[vBufB] = reg;
  189. via1[vBufB] = reg | VIA1B_vRTCClk;
  190. data = (data << 1) | (via1[vBufB] & VIA1B_vRTCData);
  191. }
  192. /* Return RTC data line to output state */
  193. via1[vDirB] |= VIA1B_vRTCData;
  194. return data;
  195. }
  196. static void via_pram_writebyte(__u8 data)
  197. {
  198. int i,reg,bit;
  199. reg = via1[vBufB] & ~(VIA1B_vRTCClk | VIA1B_vRTCData);
  200. /* The bits of the byte go in in MSB order */
  201. for (i = 0 ; i < 8 ; i++) {
  202. bit = data & 0x80? 1 : 0;
  203. data <<= 1;
  204. via1[vBufB] = reg | bit;
  205. via1[vBufB] = reg | bit | VIA1B_vRTCClk;
  206. }
  207. }
  208. /*
  209. * Execute a VIA PRAM/RTC command. For read commands
  210. * data should point to a one-byte buffer for the
  211. * resulting data. For write commands it should point
  212. * to the data byte to for the command.
  213. *
  214. * This function disables all interrupts while running.
  215. */
  216. static void via_pram_command(int command, __u8 *data)
  217. {
  218. unsigned long flags;
  219. int is_read;
  220. local_irq_save(flags);
  221. /* Enable the RTC and make sure the strobe line is high */
  222. via1[vBufB] = (via1[vBufB] | VIA1B_vRTCClk) & ~VIA1B_vRTCEnb;
  223. if (command & 0xFF00) { /* extended (two-byte) command */
  224. via_pram_writebyte((command & 0xFF00) >> 8);
  225. via_pram_writebyte(command & 0xFF);
  226. is_read = command & 0x8000;
  227. } else { /* one-byte command */
  228. via_pram_writebyte(command);
  229. is_read = command & 0x80;
  230. }
  231. if (is_read) {
  232. *data = via_pram_readbyte();
  233. } else {
  234. via_pram_writebyte(*data);
  235. }
  236. /* All done, disable the RTC */
  237. via1[vBufB] |= VIA1B_vRTCEnb;
  238. local_irq_restore(flags);
  239. }
  240. static __u8 via_read_pram(int offset)
  241. {
  242. return 0;
  243. }
  244. static void via_write_pram(int offset, __u8 data)
  245. {
  246. }
  247. /*
  248. * Return the current time in seconds since January 1, 1904.
  249. *
  250. * This only works on machines with the VIA-based PRAM/RTC, which
  251. * is basically any machine with Mac II-style ADB.
  252. */
  253. static long via_read_time(void)
  254. {
  255. union {
  256. __u8 cdata[4];
  257. long idata;
  258. } result, last_result;
  259. int ct;
  260. /*
  261. * The NetBSD guys say to loop until you get the same reading
  262. * twice in a row.
  263. */
  264. ct = 0;
  265. do {
  266. if (++ct > 10) {
  267. printk("via_read_time: couldn't get valid time, "
  268. "last read = 0x%08lx and 0x%08lx\n",
  269. last_result.idata, result.idata);
  270. break;
  271. }
  272. last_result.idata = result.idata;
  273. result.idata = 0;
  274. via_pram_command(0x81, &result.cdata[3]);
  275. via_pram_command(0x85, &result.cdata[2]);
  276. via_pram_command(0x89, &result.cdata[1]);
  277. via_pram_command(0x8D, &result.cdata[0]);
  278. } while (result.idata != last_result.idata);
  279. return result.idata - RTC_OFFSET;
  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. printk("mac_hwclk: read %04d-%02d-%-2d %02d:%02d:%02d\n",
  617. t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  618. } else { /* write */
  619. printk("mac_hwclk: tried to write %04d-%02d-%-2d %02d:%02d:%02d\n",
  620. t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  621. #if 0 /* it trashes my rtc */
  622. now = mktime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
  623. t->tm_hour, t->tm_min, t->tm_sec);
  624. switch (macintosh_config->adb_type) {
  625. case MAC_ADB_II:
  626. case MAC_ADB_IOP:
  627. via_write_time(now);
  628. break;
  629. case MAC_ADB_CUDA:
  630. cuda_write_time(now);
  631. break;
  632. case MAC_ADB_PB1:
  633. case MAC_ADB_PB2:
  634. pmu_write_time(now);
  635. break;
  636. case MAC_ADB_IISI:
  637. maciisi_write_time(now);
  638. }
  639. #endif
  640. }
  641. return 0;
  642. }
  643. /*
  644. * Set minutes/seconds in the hardware clock
  645. */
  646. int mac_set_clock_mmss (unsigned long nowtime)
  647. {
  648. struct rtc_time now;
  649. mac_hwclk(0, &now);
  650. now.tm_sec = nowtime % 60;
  651. now.tm_min = (nowtime / 60) % 60;
  652. mac_hwclk(1, &now);
  653. return 0;
  654. }