rtc-gamecube.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Nintendo GameCube, Wii and Wii U RTC driver
  4. *
  5. * This driver is for the MX23L4005, more specifically its real-time clock and
  6. * SRAM storage. The value returned by the RTC counter must be added with the
  7. * offset stored in a bias register in SRAM (on the GameCube and Wii) or in
  8. * /config/rtc.xml (on the Wii U). The latter being very impractical to access
  9. * from Linux, this driver assumes the bootloader has read it and stored it in
  10. * SRAM like for the other two consoles.
  11. *
  12. * This device sits on a bus named EXI (which is similar to SPI), channel 0,
  13. * device 1. This driver assumes no other user of the EXI bus, which is
  14. * currently the case but would have to be reworked to add support for other
  15. * GameCube hardware exposed on this bus.
  16. *
  17. * References:
  18. * - https://wiiubrew.org/wiki/Hardware/RTC
  19. * - https://wiibrew.org/wiki/MX23L4005
  20. *
  21. * Copyright (C) 2018 rw-r-r-0644
  22. * Copyright (C) 2021 Emmanuel Gil Peyrot <[email protected]>
  23. *
  24. * Based on rtc-gcn.c
  25. * Copyright (C) 2004-2009 The GameCube Linux Team
  26. * Copyright (C) 2005,2008,2009 Albert Herranz
  27. * Based on gamecube_time.c from Torben Nielsen.
  28. */
  29. #include <linux/init.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/regmap.h>
  35. #include <linux/rtc.h>
  36. #include <linux/time.h>
  37. /* EXI registers */
  38. #define EXICSR 0
  39. #define EXICR 12
  40. #define EXIDATA 16
  41. /* EXI register values */
  42. #define EXICSR_DEV 0x380
  43. #define EXICSR_DEV1 0x100
  44. #define EXICSR_CLK 0x070
  45. #define EXICSR_CLK_1MHZ 0x000
  46. #define EXICSR_CLK_2MHZ 0x010
  47. #define EXICSR_CLK_4MHZ 0x020
  48. #define EXICSR_CLK_8MHZ 0x030
  49. #define EXICSR_CLK_16MHZ 0x040
  50. #define EXICSR_CLK_32MHZ 0x050
  51. #define EXICSR_INT 0x008
  52. #define EXICSR_INTSET 0x008
  53. #define EXICR_TSTART 0x001
  54. #define EXICR_TRSMODE 0x002
  55. #define EXICR_TRSMODE_IMM 0x000
  56. #define EXICR_TRSTYPE 0x00C
  57. #define EXICR_TRSTYPE_R 0x000
  58. #define EXICR_TRSTYPE_W 0x004
  59. #define EXICR_TLEN 0x030
  60. #define EXICR_TLEN32 0x030
  61. /* EXI registers values to access the RTC */
  62. #define RTC_EXICSR (EXICSR_DEV1 | EXICSR_CLK_8MHZ | EXICSR_INTSET)
  63. #define RTC_EXICR_W (EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_W | EXICR_TLEN32)
  64. #define RTC_EXICR_R (EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_R | EXICR_TLEN32)
  65. #define RTC_EXIDATA_W 0x80000000
  66. /* RTC registers */
  67. #define RTC_COUNTER 0x200000
  68. #define RTC_SRAM 0x200001
  69. #define RTC_SRAM_BIAS 0x200004
  70. #define RTC_SNAPSHOT 0x204000
  71. #define RTC_ONTMR 0x210000
  72. #define RTC_OFFTMR 0x210001
  73. #define RTC_TEST0 0x210004
  74. #define RTC_TEST1 0x210005
  75. #define RTC_TEST2 0x210006
  76. #define RTC_TEST3 0x210007
  77. #define RTC_CONTROL0 0x21000c
  78. #define RTC_CONTROL1 0x21000d
  79. /* RTC flags */
  80. #define RTC_CONTROL0_UNSTABLE_POWER 0x00000800
  81. #define RTC_CONTROL0_LOW_BATTERY 0x00000200
  82. struct priv {
  83. struct regmap *regmap;
  84. void __iomem *iob;
  85. u32 rtc_bias;
  86. };
  87. static int exi_read(void *context, u32 reg, u32 *data)
  88. {
  89. struct priv *d = (struct priv *)context;
  90. void __iomem *iob = d->iob;
  91. /* The spin loops here loop about 15~16 times each, so there is no need
  92. * to use a more expensive sleep method.
  93. */
  94. /* Write register offset */
  95. iowrite32be(RTC_EXICSR, iob + EXICSR);
  96. iowrite32be(reg << 8, iob + EXIDATA);
  97. iowrite32be(RTC_EXICR_W, iob + EXICR);
  98. while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
  99. cpu_relax();
  100. /* Read data */
  101. iowrite32be(RTC_EXICSR, iob + EXICSR);
  102. iowrite32be(RTC_EXICR_R, iob + EXICR);
  103. while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
  104. cpu_relax();
  105. *data = ioread32be(iob + EXIDATA);
  106. /* Clear channel parameters */
  107. iowrite32be(0, iob + EXICSR);
  108. return 0;
  109. }
  110. static int exi_write(void *context, u32 reg, u32 data)
  111. {
  112. struct priv *d = (struct priv *)context;
  113. void __iomem *iob = d->iob;
  114. /* The spin loops here loop about 15~16 times each, so there is no need
  115. * to use a more expensive sleep method.
  116. */
  117. /* Write register offset */
  118. iowrite32be(RTC_EXICSR, iob + EXICSR);
  119. iowrite32be(RTC_EXIDATA_W | (reg << 8), iob + EXIDATA);
  120. iowrite32be(RTC_EXICR_W, iob + EXICR);
  121. while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
  122. cpu_relax();
  123. /* Write data */
  124. iowrite32be(RTC_EXICSR, iob + EXICSR);
  125. iowrite32be(data, iob + EXIDATA);
  126. iowrite32be(RTC_EXICR_W, iob + EXICR);
  127. while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
  128. cpu_relax();
  129. /* Clear channel parameters */
  130. iowrite32be(0, iob + EXICSR);
  131. return 0;
  132. }
  133. static const struct regmap_bus exi_bus = {
  134. /* TODO: is that true? Not that it matters here, but still. */
  135. .fast_io = true,
  136. .reg_read = exi_read,
  137. .reg_write = exi_write,
  138. };
  139. static int gamecube_rtc_read_time(struct device *dev, struct rtc_time *t)
  140. {
  141. struct priv *d = dev_get_drvdata(dev);
  142. int ret;
  143. u32 counter;
  144. time64_t timestamp;
  145. ret = regmap_read(d->regmap, RTC_COUNTER, &counter);
  146. if (ret)
  147. return ret;
  148. /* Add the counter and the bias to obtain the timestamp */
  149. timestamp = (time64_t)d->rtc_bias + counter;
  150. rtc_time64_to_tm(timestamp, t);
  151. return 0;
  152. }
  153. static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
  154. {
  155. struct priv *d = dev_get_drvdata(dev);
  156. time64_t timestamp;
  157. /* Subtract the timestamp and the bias to obtain the counter value */
  158. timestamp = rtc_tm_to_time64(t);
  159. return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
  160. }
  161. static int gamecube_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  162. {
  163. struct priv *d = dev_get_drvdata(dev);
  164. int value;
  165. int control0;
  166. int ret;
  167. switch (cmd) {
  168. case RTC_VL_READ:
  169. ret = regmap_read(d->regmap, RTC_CONTROL0, &control0);
  170. if (ret)
  171. return ret;
  172. value = 0;
  173. if (control0 & RTC_CONTROL0_UNSTABLE_POWER)
  174. value |= RTC_VL_DATA_INVALID;
  175. if (control0 & RTC_CONTROL0_LOW_BATTERY)
  176. value |= RTC_VL_BACKUP_LOW;
  177. return put_user(value, (unsigned int __user *)arg);
  178. default:
  179. return -ENOIOCTLCMD;
  180. }
  181. }
  182. static const struct rtc_class_ops gamecube_rtc_ops = {
  183. .read_time = gamecube_rtc_read_time,
  184. .set_time = gamecube_rtc_set_time,
  185. .ioctl = gamecube_rtc_ioctl,
  186. };
  187. static int gamecube_rtc_read_offset_from_sram(struct priv *d)
  188. {
  189. struct device_node *np;
  190. int ret;
  191. struct resource res;
  192. void __iomem *hw_srnprot;
  193. u32 old;
  194. np = of_find_compatible_node(NULL, NULL, "nintendo,latte-srnprot");
  195. if (!np)
  196. np = of_find_compatible_node(NULL, NULL,
  197. "nintendo,hollywood-srnprot");
  198. if (!np) {
  199. pr_info("HW_SRNPROT not found, assuming a GameCube\n");
  200. return regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
  201. }
  202. ret = of_address_to_resource(np, 0, &res);
  203. of_node_put(np);
  204. if (ret) {
  205. pr_err("no io memory range found\n");
  206. return -1;
  207. }
  208. hw_srnprot = ioremap(res.start, resource_size(&res));
  209. old = ioread32be(hw_srnprot);
  210. /* TODO: figure out why we use this magic constant. I obtained it by
  211. * reading the leftover value after boot, after IOSU already ran.
  212. *
  213. * On my Wii U, setting this register to 1 prevents the console from
  214. * rebooting properly, so wiiubrew.org must be missing something.
  215. *
  216. * See https://wiiubrew.org/wiki/Hardware/Latte_registers
  217. */
  218. if (old != 0x7bf)
  219. iowrite32be(0x7bf, hw_srnprot);
  220. /* Get the offset from RTC SRAM.
  221. *
  222. * Its default location on the GameCube and on the Wii is in the SRAM,
  223. * while on the Wii U the bootloader needs to fill it with the contents
  224. * of /config/rtc.xml on the SLC (the eMMC). We don’t do that from
  225. * Linux since it requires implementing a proprietary filesystem and do
  226. * file decryption, instead we require the bootloader to fill the same
  227. * SRAM address as on previous consoles.
  228. */
  229. ret = regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
  230. /* Reset SRAM access to how it was before, our job here is done. */
  231. if (old != 0x7bf)
  232. iowrite32be(old, hw_srnprot);
  233. iounmap(hw_srnprot);
  234. if (ret)
  235. pr_err("failed to get the RTC bias\n");
  236. return ret;
  237. }
  238. static const struct regmap_range rtc_rd_ranges[] = {
  239. regmap_reg_range(0x200000, 0x200010),
  240. regmap_reg_range(0x204000, 0x204000),
  241. regmap_reg_range(0x210000, 0x210001),
  242. regmap_reg_range(0x210004, 0x210007),
  243. regmap_reg_range(0x21000c, 0x21000d),
  244. };
  245. static const struct regmap_access_table rtc_rd_regs = {
  246. .yes_ranges = rtc_rd_ranges,
  247. .n_yes_ranges = ARRAY_SIZE(rtc_rd_ranges),
  248. };
  249. static const struct regmap_range rtc_wr_ranges[] = {
  250. regmap_reg_range(0x200000, 0x200010),
  251. regmap_reg_range(0x204000, 0x204000),
  252. regmap_reg_range(0x210000, 0x210001),
  253. regmap_reg_range(0x21000d, 0x21000d),
  254. };
  255. static const struct regmap_access_table rtc_wr_regs = {
  256. .yes_ranges = rtc_wr_ranges,
  257. .n_yes_ranges = ARRAY_SIZE(rtc_wr_ranges),
  258. };
  259. static const struct regmap_config gamecube_rtc_regmap_config = {
  260. .reg_bits = 24,
  261. .val_bits = 32,
  262. .rd_table = &rtc_rd_regs,
  263. .wr_table = &rtc_wr_regs,
  264. .max_register = 0x21000d,
  265. .name = "gamecube-rtc",
  266. };
  267. static int gamecube_rtc_probe(struct platform_device *pdev)
  268. {
  269. struct device *dev = &pdev->dev;
  270. struct rtc_device *rtc;
  271. struct priv *d;
  272. int ret;
  273. d = devm_kzalloc(dev, sizeof(struct priv), GFP_KERNEL);
  274. if (!d)
  275. return -ENOMEM;
  276. d->iob = devm_platform_ioremap_resource(pdev, 0);
  277. if (IS_ERR(d->iob))
  278. return PTR_ERR(d->iob);
  279. d->regmap = devm_regmap_init(dev, &exi_bus, d,
  280. &gamecube_rtc_regmap_config);
  281. if (IS_ERR(d->regmap))
  282. return PTR_ERR(d->regmap);
  283. ret = gamecube_rtc_read_offset_from_sram(d);
  284. if (ret)
  285. return ret;
  286. dev_dbg(dev, "SRAM bias: 0x%x", d->rtc_bias);
  287. dev_set_drvdata(dev, d);
  288. rtc = devm_rtc_allocate_device(dev);
  289. if (IS_ERR(rtc))
  290. return PTR_ERR(rtc);
  291. /* We can represent further than that, but it depends on the stored
  292. * bias and we can’t modify it persistently on all supported consoles,
  293. * so here we pretend to be limited to 2106.
  294. */
  295. rtc->range_min = 0;
  296. rtc->range_max = U32_MAX;
  297. rtc->ops = &gamecube_rtc_ops;
  298. devm_rtc_register_device(rtc);
  299. return 0;
  300. }
  301. static const struct of_device_id gamecube_rtc_of_match[] = {
  302. {.compatible = "nintendo,latte-exi" },
  303. {.compatible = "nintendo,hollywood-exi" },
  304. {.compatible = "nintendo,flipper-exi" },
  305. { }
  306. };
  307. MODULE_DEVICE_TABLE(of, gamecube_rtc_of_match);
  308. static struct platform_driver gamecube_rtc_driver = {
  309. .probe = gamecube_rtc_probe,
  310. .driver = {
  311. .name = "rtc-gamecube",
  312. .of_match_table = gamecube_rtc_of_match,
  313. },
  314. };
  315. module_platform_driver(gamecube_rtc_driver);
  316. MODULE_AUTHOR("Emmanuel Gil Peyrot <[email protected]>");
  317. MODULE_DESCRIPTION("Nintendo GameCube, Wii and Wii U RTC driver");
  318. MODULE_LICENSE("GPL");