ugecon.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * arch/powerpc/boot/ugecon.c
  3. *
  4. * USB Gecko bootwrapper console.
  5. * Copyright (C) 2008-2009 The GameCube Linux Team
  6. * Copyright (C) 2008,2009 Albert Herranz
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. */
  14. #include <stddef.h>
  15. #include "stdio.h"
  16. #include "types.h"
  17. #include "io.h"
  18. #include "ops.h"
  19. #define EXI_CLK_32MHZ 5
  20. #define EXI_CSR 0x00
  21. #define EXI_CSR_CLKMASK (0x7<<4)
  22. #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4)
  23. #define EXI_CSR_CSMASK (0x7<<7)
  24. #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */
  25. #define EXI_CR 0x0c
  26. #define EXI_CR_TSTART (1<<0)
  27. #define EXI_CR_WRITE (1<<2)
  28. #define EXI_CR_READ_WRITE (2<<2)
  29. #define EXI_CR_TLEN(len) (((len)-1)<<4)
  30. #define EXI_DATA 0x10
  31. /* virtual address base for input/output, retrieved from device tree */
  32. static void *ug_io_base;
  33. static u32 ug_io_transaction(u32 in)
  34. {
  35. u32 *csr_reg = ug_io_base + EXI_CSR;
  36. u32 *data_reg = ug_io_base + EXI_DATA;
  37. u32 *cr_reg = ug_io_base + EXI_CR;
  38. u32 csr, data, cr;
  39. /* select */
  40. csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
  41. out_be32(csr_reg, csr);
  42. /* read/write */
  43. data = in;
  44. out_be32(data_reg, data);
  45. cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
  46. out_be32(cr_reg, cr);
  47. while (in_be32(cr_reg) & EXI_CR_TSTART)
  48. barrier();
  49. /* deselect */
  50. out_be32(csr_reg, 0);
  51. data = in_be32(data_reg);
  52. return data;
  53. }
  54. static int ug_is_txfifo_ready(void)
  55. {
  56. return ug_io_transaction(0xc0000000) & 0x04000000;
  57. }
  58. static void ug_raw_putc(char ch)
  59. {
  60. ug_io_transaction(0xb0000000 | (ch << 20));
  61. }
  62. static void ug_putc(char ch)
  63. {
  64. int count = 16;
  65. if (!ug_io_base)
  66. return;
  67. while (!ug_is_txfifo_ready() && count--)
  68. barrier();
  69. if (count >= 0)
  70. ug_raw_putc(ch);
  71. }
  72. void ug_console_write(const char *buf, int len)
  73. {
  74. char *b = (char *)buf;
  75. while (len--) {
  76. if (*b == '\n')
  77. ug_putc('\r');
  78. ug_putc(*b++);
  79. }
  80. }
  81. static int ug_is_adapter_present(void)
  82. {
  83. if (!ug_io_base)
  84. return 0;
  85. return ug_io_transaction(0x90000000) == 0x04700000;
  86. }
  87. static void *ug_grab_exi_io_base(void)
  88. {
  89. u32 v;
  90. void *devp;
  91. devp = find_node_by_compatible(NULL, "nintendo,flipper-exi");
  92. if (devp == NULL)
  93. goto err_out;
  94. if (getprop(devp, "virtual-reg", &v, sizeof(v)) != sizeof(v))
  95. goto err_out;
  96. return (void *)v;
  97. err_out:
  98. return NULL;
  99. }
  100. void *ug_probe(void)
  101. {
  102. void *exi_io_base;
  103. int i;
  104. exi_io_base = ug_grab_exi_io_base();
  105. if (!exi_io_base)
  106. return NULL;
  107. /* look for a usbgecko on memcard slots A and B */
  108. for (i = 0; i < 2; i++) {
  109. ug_io_base = exi_io_base + 0x14 * i;
  110. if (ug_is_adapter_present())
  111. break;
  112. }
  113. if (i == 2)
  114. ug_io_base = NULL;
  115. return ug_io_base;
  116. }