Sony PlayStation2 Study
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
7.3 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (C) 2014 Juergen Urban
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <netdev.h>
  8. #include <rtc.h>
  9. #include <serial.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/io.h>
  12. #include <asm/ps2.h>
  13. #include <asm/global_data.h>
  14. #include "bootinfo.h"
  15. #include "sbios.h"
  16. /* EE SIO registers */
  17. #define EE_SIO_BASE 0x1000f100
  18. #define EE_SIO_SIZE 0x100
  19. #define EE_SIO_LCR 0x1000f100 /* Line Control Register */
  20. #define LCR_UMODE_8BIT 0x00
  21. #define LCR_UMODE_7BIT 0x01
  22. #define LCR_USBL_1BIT 0x00 /* UART Stop Bit Length */
  23. #define LCR_USBL_2BITS 0x04
  24. #define LCR_UPEN_OFF 0x00 /* UART Parity Enable */
  25. #define LCR_UPEN_ON 0x08
  26. #define LCR_UEPS_ODD 0x00 /* UART Even Parity Select */
  27. #define LCR_UEPS_EVEN 0x10
  28. #define LCR_SCS_VAL 0x20 /* Baud rate generator output that divided CPUCLK */
  29. #define EE_SIO_LSR 0x1000f110 /* Line Status Register */
  30. #define LSR_OE 0x02 /* Overrun error */
  31. #define LSR_PE 0x04 /* Parity error */
  32. #define LSR_FE 0x08 /* Framing error */
  33. #define EE_SIO_IER 0x1000f120 /* Interrupt Enable Register */
  34. #define IER_RX_DATA 0x0001
  35. #define IER_TX_EMPTY 0x0002
  36. #define IER_BI 0x0004 /* Break signal */
  37. #define EE_SIO_ISR 0x1000f130 /* Interrupt Status Register */
  38. #define ISR_RX_DATA 0x0001
  39. #define ISR_TX_EMPTY 0x0002
  40. #define ISR_BI 0x0004 /* Break signal */
  41. #define ISR_TXFIFO_MASK 0xf000
  42. #define ISR_TXFIFO_FULL 0x8000
  43. #define ISR_RXFIFO_MASK 0x0f00
  44. #define EE_SIO_FCR 0x1000f140 /* FIFO Control Register */
  45. #define FCR_FRSTE 0x01 /* FIFO Reset Enable */
  46. #define FCR_RFRST 0x02 /* RX FIFO Reset */
  47. #define SIO_FCR_TFRST 0x04 /* TX FIFO Reset */
  48. #define SIO_BGR 0x1000f150 /* Baud Rate Control Register */
  49. #define EE_SIO_TXFIFO 0x1000f180
  50. #define EE_SIO_RXFIFO 0x1000f1c0
  51. #define EE_DMAC_SIF0_CHCR 0x1000c000
  52. #define CHCR_STR 0x0100
  53. #define SBIOS_BASE 0x80001000
  54. #define SBIOS_MAGIC 0x80001004
  55. #define SBIOS_MAGICVAL 0x62325350
  56. /* Base address for IOP memory. */
  57. #define PS2_IOP_HEAP_BASE 0x1c000000
  58. #define PS2_IOP_HEAP_END 0x20000000
  59. DECLARE_GLOBAL_DATA_PTR;
  60. int sbios(int func, void *arg)
  61. {
  62. extern int sbios_call(int, void *, void *);
  63. if (gd->arch._sbios == NULL) {
  64. error("SBIOS not found\n");
  65. return -1;
  66. }
  67. return sbios_call(func, arg, gd->arch._sbios);
  68. }
  69. phys_size_t initdram(int board_type)
  70. {
  71. return CONFIG_SYS_MEM_SIZE;
  72. }
  73. static void check_sif_irq(void)
  74. {
  75. if (inl(EE_DMAC_SIF0_CHCR) & CHCR_STR) {
  76. /* DMA not finished, no interrupt. */
  77. return;
  78. }
  79. /* Emulate interrupt. */
  80. sbios(SB_SIFCMDINTRHDLR, NULL);
  81. }
  82. static void rpc_wakeup(void *p, int result)
  83. {
  84. volatile int *woken;
  85. woken = (int *) p;
  86. *woken = 1;
  87. }
  88. static int sbios_rpc(int func, void *arg, int *result)
  89. {
  90. int ret;
  91. struct sbr_common_arg carg;
  92. volatile int woken = 0;
  93. carg.arg = arg;
  94. carg.func = rpc_wakeup;
  95. carg.para = (void *) &woken;
  96. carg.result = -1;
  97. do {
  98. debug("Call SBIOS %d\n", func);
  99. ret = sbios(func, &carg);
  100. debug("ret = %d\n", ret);
  101. switch (ret) {
  102. case 0:
  103. break;
  104. case -SIF_RPCE_SENDP:
  105. /* resource temporarily unavailable */
  106. /* Time to check for interrupts. */
  107. check_sif_irq();
  108. break;
  109. default:
  110. *result = ret;
  111. error("sbios_rpc: RPC failed, func=%d result=%d\n", func, ret);
  112. return ret;
  113. }
  114. } while (ret < 0);
  115. while(woken == 0) {
  116. debug("check_sif_irq\n");
  117. /* Time to check for interrupts. */
  118. check_sif_irq();
  119. }
  120. debug("carg.result %d\n", carg.result);
  121. *result = carg.result;
  122. return 0;
  123. }
  124. static void driver_init(void)
  125. {
  126. int rv;
  127. int result;
  128. rv = sbios(SB_SIFINIT, NULL);
  129. if (rv < 0) {
  130. error("SB_SIFINIT failed rv = %d\n", rv);
  131. return;
  132. }
  133. rv = sbios(SB_SIFINITCMD, NULL);
  134. if (rv < 0) {
  135. error("SB_SIFINITCMD failed rv = %d\n", rv);
  136. return;
  137. }
  138. rv = sbios(SB_SIFINITRPC, NULL);
  139. if (rv < 0) {
  140. error("SB_SIFINITRPC failed rv = %d\n", rv);
  141. return;
  142. }
  143. for (;;) {
  144. debug("Call SBR_IOPH_INIT\n");
  145. rv = sbios_rpc(SBR_IOPH_INIT, NULL, &result);
  146. debug("rv = %d\n", rv);
  147. if (rv < 0) {
  148. error("SBR_IOPH_INIT failed rv = %d\n", rv);
  149. return;
  150. }
  151. debug("result = %d\n", result);
  152. if (result == 0) {
  153. /* Success */
  154. break;
  155. }
  156. }
  157. debug("SBIOS drivers successfully initialized.\n");
  158. }
  159. int checkboard(void)
  160. {
  161. struct ps2_bootinfo *info = (void *) CKSEG0ADDR(PS2_BOOTINFO_OLDADDR);
  162. uint32_t size;
  163. volatile uint32_t *sbios_magic;
  164. int sbversion = 0;
  165. struct ps2_bootinfo bootinfo;
  166. puts("Board: Sony Playstation 2 MIPS r5900\n");
  167. memset(&bootinfo, 0, sizeof(bootinfo));
  168. size = info->size;
  169. if (size > sizeof(bootinfo)) {
  170. size = sizeof(bootinfo);
  171. }
  172. memcpy(&bootinfo, info, size);
  173. sbios_magic = (uint32_t *) SBIOS_MAGIC;
  174. if (*sbios_magic == SBIOS_MAGICVAL) {
  175. gd->arch._sbios = *(int (**)(int, void *))(SBIOS_BASE);
  176. }
  177. else
  178. gd->arch._sbios = NULL;
  179. sbversion = sbios(SB_GETVER, NULL);
  180. printf("SBIOS Version 0x%08x\n", sbversion);
  181. driver_init();
  182. return 0;
  183. }
  184. int board_eth_init(bd_t *bis)
  185. {
  186. /* TBD */
  187. return 0;
  188. }
  189. void _machine_restart(void)
  190. {
  191. struct sb_halt_arg arg;
  192. arg.mode = SB_HALT_MODE_PWROFF;
  193. sbios(SB_HALT, &arg);
  194. }
  195. int board_early_init_f(void)
  196. {
  197. return 0;
  198. }
  199. int sio_start(void)
  200. {
  201. return 0;
  202. }
  203. int sio_stop(void)
  204. {
  205. return 0;
  206. }
  207. void sio_setbrg(void)
  208. {
  209. /* TBD: Configure baudrate */
  210. }
  211. int sio_getc(void)
  212. {
  213. uint32_t isr;
  214. uint8_t ch;
  215. while((((isr = inl(EE_SIO_ISR)) & ISR_RXFIFO_MASK) >> 8) == 0) {
  216. /* Wait for input */
  217. }
  218. ch = inb(EE_SIO_RXFIFO);
  219. return ch;
  220. }
  221. int sio_tstc(void)
  222. {
  223. /* Return number of characters in RX FIFO. */
  224. return (((inl(EE_SIO_ISR)) & ISR_RXFIFO_MASK) >> 8);
  225. }
  226. void sio_putc(const char c)
  227. {
  228. if (c == '\n')
  229. sio_putc('\r');
  230. while ((inl(EE_SIO_ISR) & ISR_TXFIFO_MASK) == ISR_TXFIFO_FULL) {
  231. /* Wait until data can be send. */
  232. };
  233. outb(c, EE_SIO_TXFIFO);
  234. }
  235. void sio_puts(const char *s)
  236. {
  237. while(*s != 0) {
  238. sio_putc(*s);
  239. s++;
  240. }
  241. }
  242. struct serial_device sio_device = {
  243. .name = "PS2SIO",
  244. .start = sio_start,
  245. .stop = sio_stop,
  246. .setbrg = sio_setbrg,
  247. .getc = sio_getc,
  248. .tstc = sio_tstc,
  249. .putc = sio_putc,
  250. .puts = sio_puts,
  251. };
  252. struct serial_device *default_serial_console(void)
  253. {
  254. set_io_port_base(CKSEG1ADDR(0));
  255. return &sio_device;
  256. }
  257. void rtc_reset (void)
  258. {
  259. /* TBD */
  260. return;
  261. }
  262. int rtc_get (struct rtc_time *tmp)
  263. {
  264. /* TBD */
  265. return 0;
  266. }
  267. int rtc_set (struct rtc_time *tmp)
  268. {
  269. /* TBD */
  270. return 0;
  271. }
  272. /** Allocate bus address. Returns 0 on error. */
  273. uint32_t ps2sif_allociopheap(int size)
  274. {
  275. struct sbr_ioph_alloc_arg arg;
  276. int result;
  277. int err;
  278. arg.size = size;
  279. err = sbios_rpc(SBR_IOPH_ALLOC, &arg, &result);
  280. if (err < 0) {
  281. return 0;
  282. }
  283. return result;
  284. }
  285. int ps2sif_freeiopheap(uint32_t busaddr)
  286. {
  287. struct sbr_ioph_free_arg arg;
  288. int result;
  289. int err;
  290. arg.addr = busaddr;
  291. err = sbios_rpc(SBR_IOPH_FREE, &arg, &result);
  292. if (err < 0) {
  293. return -1;
  294. }
  295. return result;
  296. }
  297. uint32_t ps2sif_phystobus(uint32_t physaddr)
  298. {
  299. if (physaddr <= PS2_IOP_HEAP_BASE) {
  300. error("Bad physical address 0x%08x used\n",
  301. physaddr);
  302. }
  303. if (physaddr >= PS2_IOP_HEAP_END) {
  304. error("Bad physical address 0x%08x used\n",
  305. physaddr);
  306. }
  307. return (physaddr - PS2_IOP_HEAP_BASE);
  308. }
  309. /** Converts iopheap bus addr to phys addr for use with inl() and outl(). */
  310. uint32_t ps2sif_bustophys(uint32_t busaddr)
  311. {
  312. uint32_t physaddr;
  313. assert(busaddr != 0);
  314. physaddr = busaddr + PS2_IOP_HEAP_BASE;
  315. if (physaddr < PS2_IOP_HEAP_BASE) {
  316. error("Bad bus address 0x%08x used\n",
  317. busaddr);
  318. }
  319. if (physaddr >= PS2_IOP_HEAP_END) {
  320. error("Bad bus address 0x%08x used\n",
  321. busaddr);
  322. }
  323. return physaddr;
  324. }