usbgecko_udbg.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <asm/fixmap.h>
  19. #include "usbgecko_udbg.h"
  20. #define EXI_CLK_32MHZ 5
  21. #define EXI_CSR 0x00
  22. #define EXI_CSR_CLKMASK (0x7<<4)
  23. #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4)
  24. #define EXI_CSR_CSMASK (0x7<<7)
  25. #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */
  26. #define EXI_CR 0x0c
  27. #define EXI_CR_TSTART (1<<0)
  28. #define EXI_CR_WRITE (1<<2)
  29. #define EXI_CR_READ_WRITE (2<<2)
  30. #define EXI_CR_TLEN(len) (((len)-1)<<4)
  31. #define EXI_DATA 0x10
  32. #define UG_READ_ATTEMPTS 100
  33. #define UG_WRITE_ATTEMPTS 100
  34. static void __iomem *ug_io_base;
  35. /*
  36. * Performs one input/output transaction between the exi host and the usbgecko.
  37. */
  38. static u32 ug_io_transaction(u32 in)
  39. {
  40. u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
  41. u32 __iomem *data_reg = ug_io_base + EXI_DATA;
  42. u32 __iomem *cr_reg = ug_io_base + EXI_CR;
  43. u32 csr, data, cr;
  44. /* select */
  45. csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
  46. out_be32(csr_reg, csr);
  47. /* read/write */
  48. data = in;
  49. out_be32(data_reg, data);
  50. cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
  51. out_be32(cr_reg, cr);
  52. while (in_be32(cr_reg) & EXI_CR_TSTART)
  53. barrier();
  54. /* deselect */
  55. out_be32(csr_reg, 0);
  56. /* result */
  57. data = in_be32(data_reg);
  58. return data;
  59. }
  60. /*
  61. * Returns true if an usbgecko adapter is found.
  62. */
  63. static int ug_is_adapter_present(void)
  64. {
  65. if (!ug_io_base)
  66. return 0;
  67. return ug_io_transaction(0x90000000) == 0x04700000;
  68. }
  69. /*
  70. * Returns true if the TX fifo is ready for transmission.
  71. */
  72. static int ug_is_txfifo_ready(void)
  73. {
  74. return ug_io_transaction(0xc0000000) & 0x04000000;
  75. }
  76. /*
  77. * Tries to transmit a character.
  78. * If the TX fifo is not ready the result is undefined.
  79. */
  80. static void ug_raw_putc(char ch)
  81. {
  82. ug_io_transaction(0xb0000000 | (ch << 20));
  83. }
  84. /*
  85. * Transmits a character.
  86. * It silently fails if the TX fifo is not ready after a number of retries.
  87. */
  88. static void ug_putc(char ch)
  89. {
  90. int count = UG_WRITE_ATTEMPTS;
  91. if (!ug_io_base)
  92. return;
  93. if (ch == '\n')
  94. ug_putc('\r');
  95. while (!ug_is_txfifo_ready() && count--)
  96. barrier();
  97. if (count >= 0)
  98. ug_raw_putc(ch);
  99. }
  100. /*
  101. * Returns true if the RX fifo is ready for transmission.
  102. */
  103. static int ug_is_rxfifo_ready(void)
  104. {
  105. return ug_io_transaction(0xd0000000) & 0x04000000;
  106. }
  107. /*
  108. * Tries to receive a character.
  109. * If a character is unavailable the function returns -1.
  110. */
  111. static int ug_raw_getc(void)
  112. {
  113. u32 data = ug_io_transaction(0xa0000000);
  114. if (data & 0x08000000)
  115. return (data >> 16) & 0xff;
  116. else
  117. return -1;
  118. }
  119. /*
  120. * Receives a character.
  121. * It fails if the RX fifo is not ready after a number of retries.
  122. */
  123. static int ug_getc(void)
  124. {
  125. int count = UG_READ_ATTEMPTS;
  126. if (!ug_io_base)
  127. return -1;
  128. while (!ug_is_rxfifo_ready() && count--)
  129. barrier();
  130. return ug_raw_getc();
  131. }
  132. /*
  133. * udbg functions.
  134. *
  135. */
  136. /*
  137. * Transmits a character.
  138. */
  139. void ug_udbg_putc(char ch)
  140. {
  141. ug_putc(ch);
  142. }
  143. /*
  144. * Receives a character. Waits until a character is available.
  145. */
  146. static int ug_udbg_getc(void)
  147. {
  148. int ch;
  149. while ((ch = ug_getc()) == -1)
  150. barrier();
  151. return ch;
  152. }
  153. /*
  154. * Receives a character. If a character is not available, returns -1.
  155. */
  156. static int ug_udbg_getc_poll(void)
  157. {
  158. if (!ug_is_rxfifo_ready())
  159. return -1;
  160. return ug_getc();
  161. }
  162. /*
  163. * Retrieves and prepares the virtual address needed to access the hardware.
  164. */
  165. static void __iomem *ug_udbg_setup_exi_io_base(struct device_node *np)
  166. {
  167. void __iomem *exi_io_base = NULL;
  168. phys_addr_t paddr;
  169. const unsigned int *reg;
  170. reg = of_get_property(np, "reg", NULL);
  171. if (reg) {
  172. paddr = of_translate_address(np, reg);
  173. if (paddr)
  174. exi_io_base = ioremap(paddr, reg[1]);
  175. }
  176. return exi_io_base;
  177. }
  178. /*
  179. * Checks if a USB Gecko adapter is inserted in any memory card slot.
  180. */
  181. static void __iomem *ug_udbg_probe(void __iomem *exi_io_base)
  182. {
  183. int i;
  184. /* look for a usbgecko on memcard slots A and B */
  185. for (i = 0; i < 2; i++) {
  186. ug_io_base = exi_io_base + 0x14 * i;
  187. if (ug_is_adapter_present())
  188. break;
  189. }
  190. if (i == 2)
  191. ug_io_base = NULL;
  192. return ug_io_base;
  193. }
  194. /*
  195. * USB Gecko udbg support initialization.
  196. */
  197. void __init ug_udbg_init(void)
  198. {
  199. struct device_node *np;
  200. void __iomem *exi_io_base;
  201. if (ug_io_base)
  202. udbg_printf("%s: early -> final\n", __func__);
  203. np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");
  204. if (!np) {
  205. udbg_printf("%s: EXI node not found\n", __func__);
  206. goto out;
  207. }
  208. exi_io_base = ug_udbg_setup_exi_io_base(np);
  209. if (!exi_io_base) {
  210. udbg_printf("%s: failed to setup EXI io base\n", __func__);
  211. goto done;
  212. }
  213. if (!ug_udbg_probe(exi_io_base)) {
  214. udbg_printf("usbgecko_udbg: not found\n");
  215. iounmap(exi_io_base);
  216. } else {
  217. udbg_putc = ug_udbg_putc;
  218. udbg_getc = ug_udbg_getc;
  219. udbg_getc_poll = ug_udbg_getc_poll;
  220. udbg_printf("usbgecko_udbg: ready\n");
  221. }
  222. done:
  223. of_node_put(np);
  224. out:
  225. return;
  226. }
  227. #ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
  228. static phys_addr_t __init ug_early_grab_io_addr(void)
  229. {
  230. #if defined(CONFIG_GAMECUBE)
  231. return 0x0c000000;
  232. #elif defined(CONFIG_WII)
  233. return 0x0d000000;
  234. #else
  235. #error Invalid platform for USB Gecko based early debugging.
  236. #endif
  237. }
  238. /*
  239. * USB Gecko early debug support initialization for udbg.
  240. */
  241. void __init udbg_init_usbgecko(void)
  242. {
  243. void __iomem *early_debug_area;
  244. void __iomem *exi_io_base;
  245. /*
  246. * At this point we have a BAT already setup that enables I/O
  247. * to the EXI hardware.
  248. *
  249. * The BAT uses a virtual address range reserved at the fixmap.
  250. * This must match the virtual address configured in
  251. * head_32.S:setup_usbgecko_bat().
  252. */
  253. early_debug_area = (void __iomem *)__fix_to_virt(FIX_EARLY_DEBUG_BASE);
  254. exi_io_base = early_debug_area + 0x00006800;
  255. /* try to detect a USB Gecko */
  256. if (!ug_udbg_probe(exi_io_base))
  257. return;
  258. /* we found a USB Gecko, load udbg hooks */
  259. udbg_putc = ug_udbg_putc;
  260. udbg_getc = ug_udbg_getc;
  261. udbg_getc_poll = ug_udbg_getc_poll;
  262. /*
  263. * Prepare again the same BAT for MMU_init.
  264. * This allows udbg I/O to continue working after the MMU is
  265. * turned on for real.
  266. * It is safe to continue using the same virtual address as it is
  267. * a reserved fixmap area.
  268. */
  269. setbat(1, (unsigned long)early_debug_area,
  270. ug_early_grab_io_addr(), 128*1024, PAGE_KERNEL_NCG);
  271. }
  272. #endif /* CONFIG_PPC_EARLY_DEBUG_USBGECKO */