usbgecko_udbg.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
  3. *
  4. * udbg serial input/output routines for the USB Gecko adapter.
  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 <mm/mmu_decl.h>
  15. #include <asm/io.h>
  16. #include <asm/prom.h>
  17. #include <asm/udbg.h>
  18. #include "usbgecko_udbg.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. #define UG_READ_ATTEMPTS 100
  32. #define UG_WRITE_ATTEMPTS 100
  33. static void __iomem *ug_io_base;
  34. /*
  35. * Performs one input/output transaction between the exi host and the usbgecko.
  36. */
  37. static u32 ug_io_transaction(u32 in)
  38. {
  39. u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
  40. u32 __iomem *data_reg = ug_io_base + EXI_DATA;
  41. u32 __iomem *cr_reg = ug_io_base + EXI_CR;
  42. u32 csr, data, cr;
  43. /* select */
  44. csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
  45. out_be32(csr_reg, csr);
  46. /* read/write */
  47. data = in;
  48. out_be32(data_reg, data);
  49. cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
  50. out_be32(cr_reg, cr);
  51. while (in_be32(cr_reg) & EXI_CR_TSTART)
  52. barrier();
  53. /* deselect */
  54. out_be32(csr_reg, 0);
  55. /* result */
  56. data = in_be32(data_reg);
  57. return data;
  58. }
  59. /*
  60. * Returns true if an usbgecko adapter is found.
  61. */
  62. static int ug_is_adapter_present(void)
  63. {
  64. if (!ug_io_base)
  65. return 0;
  66. return ug_io_transaction(0x90000000) == 0x04700000;
  67. }
  68. /*
  69. * Returns true if the TX fifo is ready for transmission.
  70. */
  71. static int ug_is_txfifo_ready(void)
  72. {
  73. return ug_io_transaction(0xc0000000) & 0x04000000;
  74. }
  75. /*
  76. * Tries to transmit a character.
  77. * If the TX fifo is not ready the result is undefined.
  78. */
  79. static void ug_raw_putc(char ch)
  80. {
  81. ug_io_transaction(0xb0000000 | (ch << 20));
  82. }
  83. /*
  84. * Transmits a character.
  85. * It silently fails if the TX fifo is not ready after a number of retries.
  86. */
  87. static void ug_putc(char ch)
  88. {
  89. int count = UG_WRITE_ATTEMPTS;
  90. if (!ug_io_base)
  91. return;
  92. if (ch == '\n')
  93. ug_putc('\r');
  94. while (!ug_is_txfifo_ready() && count--)
  95. barrier();
  96. if (count)
  97. ug_raw_putc(ch);
  98. }
  99. /*
  100. * Returns true if the RX fifo is ready for transmission.
  101. */
  102. static int ug_is_rxfifo_ready(void)
  103. {
  104. return ug_io_transaction(0xd0000000) & 0x04000000;
  105. }
  106. /*
  107. * Tries to receive a character.
  108. * If a character is unavailable the function returns -1.
  109. */
  110. static int ug_raw_getc(void)
  111. {
  112. u32 data = ug_io_transaction(0xa0000000);
  113. if (data & 0x08000000)
  114. return (data >> 16) & 0xff;
  115. else
  116. return -1;
  117. }
  118. /*
  119. * Receives a character.
  120. * It fails if the RX fifo is not ready after a number of retries.
  121. */
  122. static int ug_getc(void)
  123. {
  124. int count = UG_READ_ATTEMPTS;
  125. if (!ug_io_base)
  126. return -1;
  127. while (!ug_is_rxfifo_ready() && count--)
  128. barrier();
  129. return ug_raw_getc();
  130. }
  131. /*
  132. * udbg functions.
  133. *
  134. */
  135. /*
  136. * Transmits a character.
  137. */
  138. void ug_udbg_putc(char ch)
  139. {
  140. ug_putc(ch);
  141. }
  142. /*
  143. * Receives a character. Waits until a character is available.
  144. */
  145. static int ug_udbg_getc(void)
  146. {
  147. int ch;
  148. while ((ch = ug_getc()) == -1)
  149. barrier();
  150. return ch;
  151. }
  152. /*
  153. * Receives a character. If a character is not available, returns -1.
  154. */
  155. static int ug_udbg_getc_poll(void)
  156. {
  157. if (!ug_is_rxfifo_ready())
  158. return -1;
  159. return ug_getc();
  160. }
  161. /*
  162. * Retrieves and prepares the virtual address needed to access the hardware.
  163. */
  164. static void __iomem *ug_udbg_setup_exi_io_base(struct device_node *np)
  165. {
  166. void __iomem *exi_io_base = NULL;
  167. phys_addr_t paddr;
  168. const unsigned int *reg;
  169. reg = of_get_property(np, "reg", NULL);
  170. if (reg) {
  171. paddr = of_translate_address(np, reg);
  172. if (paddr)
  173. exi_io_base = ioremap(paddr, reg[1]);
  174. }
  175. return exi_io_base;
  176. }
  177. /*
  178. * Checks if a USB Gecko adapter is inserted in any memory card slot.
  179. */
  180. static void __iomem *ug_udbg_probe(void __iomem *exi_io_base)
  181. {
  182. int i;
  183. /* look for a usbgecko on memcard slots A and B */
  184. for (i = 0; i < 2; i++) {
  185. ug_io_base = exi_io_base + 0x14 * i;
  186. if (ug_is_adapter_present())
  187. break;
  188. }
  189. if (i == 2)
  190. ug_io_base = NULL;
  191. return ug_io_base;
  192. }
  193. /*
  194. * USB Gecko udbg support initialization.
  195. */
  196. void __init ug_udbg_init(void)
  197. {
  198. struct device_node *np;
  199. void __iomem *exi_io_base;
  200. if (ug_io_base)
  201. udbg_printf("%s: early -> final\n", __func__);
  202. np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");
  203. if (!np) {
  204. udbg_printf("%s: EXI node not found\n", __func__);
  205. goto done;
  206. }
  207. exi_io_base = ug_udbg_setup_exi_io_base(np);
  208. if (!exi_io_base) {
  209. udbg_printf("%s: failed to setup EXI io base\n", __func__);
  210. goto done;
  211. }
  212. if (!ug_udbg_probe(exi_io_base)) {
  213. udbg_printf("usbgecko_udbg: not found\n");
  214. iounmap(exi_io_base);
  215. } else {
  216. udbg_putc = ug_udbg_putc;
  217. udbg_getc = ug_udbg_getc;
  218. udbg_getc_poll = ug_udbg_getc_poll;
  219. udbg_printf("usbgecko_udbg: ready\n");
  220. }
  221. done:
  222. if (np)
  223. of_node_put(np);
  224. return;
  225. }