misc.c 14 KB

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