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