Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / arch / powerpc / platforms / wsp / h8.c
1 /*
2  * Copyright 2008-2011, IBM Corporation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/of.h>
12 #include <linux/io.h>
13 #include <linux/of_address.h>
14
15 #include "wsp.h"
16
17 /*
18  * The UART connection to the H8 is over ttyS1 which is just a 16550.
19  * We assume that FW has it setup right and no one messes with it.
20  */
21
22
23 static u8 __iomem *h8;
24
25 #define RBR 0           /* Receiver Buffer Register */
26 #define THR 0           /* Transmitter Holding Register */
27 #define LSR 5           /* Line Status Register */
28 #define LSR_DR 0x01     /* LSR value for Data-Ready */
29 #define LSR_THRE 0x20   /* LSR value for Transmitter-Holding-Register-Empty */
30 static void wsp_h8_putc(int c)
31 {
32         u8 lsr;
33
34         do {
35                 lsr = readb(h8 + LSR);
36         } while ((lsr & LSR_THRE) != LSR_THRE);
37         writeb(c, h8 + THR);
38 }
39
40 static int wsp_h8_getc(void)
41 {
42         u8 lsr;
43
44         do {
45                 lsr = readb(h8 + LSR);
46         } while ((lsr & LSR_DR) != LSR_DR);
47
48         return readb(h8 + RBR);
49 }
50
51 static void wsp_h8_puts(const char *s, int sz)
52 {
53         int i;
54
55         for (i = 0; i < sz; i++) {
56                 wsp_h8_putc(s[i]);
57
58                 /* no flow control so wait for echo */
59                 wsp_h8_getc();
60         }
61         wsp_h8_putc('\r');
62         wsp_h8_putc('\n');
63 }
64
65 static void wsp_h8_terminal_cmd(const char *cmd, int sz)
66 {
67         hard_irq_disable();
68         wsp_h8_puts(cmd, sz);
69         /* should never return, but just in case */
70         for (;;)
71                 continue;
72 }
73
74
75 void wsp_h8_restart(char *cmd)
76 {
77         static const char restart[] = "warm-reset";
78
79         (void)cmd;
80         wsp_h8_terminal_cmd(restart, sizeof(restart) - 1);
81 }
82
83 void wsp_h8_power_off(void)
84 {
85         static const char off[] = "power-off";
86
87         wsp_h8_terminal_cmd(off, sizeof(off) - 1);
88 }
89
90 static void __iomem *wsp_h8_getaddr(void)
91 {
92         struct device_node *aliases;
93         struct device_node *uart;
94         struct property *path;
95         void __iomem *va = NULL;
96
97         /*
98          * there is nothing in the devtree to tell us which is mapped
99          * to the H8, but se know it is the second serial port.
100          */
101
102         aliases = of_find_node_by_path("/aliases");
103         if (aliases == NULL)
104                 return NULL;
105
106         path = of_find_property(aliases, "serial1", NULL);
107         if (path == NULL)
108                 goto out;
109
110         uart = of_find_node_by_path(path->value);
111         if (uart == NULL)
112                 goto out;
113
114         va = of_iomap(uart, 0);
115
116         /* remove it so no one messes with it */
117         of_detach_node(uart);
118         of_node_put(uart);
119
120 out:
121         of_node_put(aliases);
122
123         return va;
124 }
125
126 void __init wsp_setup_h8(void)
127 {
128         h8 = wsp_h8_getaddr();
129
130         /* Devtree change? lets hard map it anyway */
131         if (h8 == NULL) {
132                 pr_warn("UART to H8 could not be found");
133                 h8 = ioremap(0xffc0008000ULL, 0x100);
134         }
135 }