[ARM] Orion: remove code duplication in TS209 and TS409 setup files
[safe/jmp/linux-2.6] / arch / arm / mach-orion5x / tsx09-common.c
1 /*
2  * QNAP TS-x09 Boards common functions
3  *
4  * Maintainers: Lennert Buytenhek <buytenh@marvell.com>
5  *              Byron Bradley <byron.bbradley@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/mv643xx_eth.h>
16 #include <linux/serial_reg.h>
17 #include "tsx09-common.h"
18
19 /*****************************************************************************
20  * QNAP TS-x09 specific power off method via UART1-attached PIC
21  ****************************************************************************/
22
23 #define UART1_REG(x)    (UART1_VIRT_BASE + ((UART_##x) << 2))
24
25 void qnap_tsx09_power_off(void)
26 {
27         /* 19200 baud divisor */
28         const unsigned divisor = ((ORION5X_TCLK + (8 * 19200)) / (16 * 19200));
29
30         pr_info("%s: triggering power-off...\n", __func__);
31
32         /* hijack uart1 and reset into sane state (19200,8n1) */
33         writel(0x83, UART1_REG(LCR));
34         writel(divisor & 0xff, UART1_REG(DLL));
35         writel((divisor >> 8) & 0xff, UART1_REG(DLM));
36         writel(0x03, UART1_REG(LCR));
37         writel(0x00, UART1_REG(IER));
38         writel(0x00, UART1_REG(FCR));
39         writel(0x00, UART1_REG(MCR));
40
41         /* send the power-off command 'A' to PIC */
42         writel('A', UART1_REG(TX));
43 }
44
45 /*****************************************************************************
46  * Ethernet
47  ****************************************************************************/
48
49 struct mv643xx_eth_platform_data qnap_tsx09_eth_data = {
50         .phy_addr       = 8,
51 };
52
53 static int __init qnap_tsx09_parse_hex_nibble(char n)
54 {
55         if (n >= '0' && n <= '9')
56                 return n - '0';
57
58         if (n >= 'A' && n <= 'F')
59                 return n - 'A' + 10;
60
61         if (n >= 'a' && n <= 'f')
62                 return n - 'a' + 10;
63
64         return -1;
65 }
66
67 static int __init qnap_tsx09_parse_hex_byte(const char *b)
68 {
69         int hi;
70         int lo;
71
72         hi = qnap_tsx09_parse_hex_nibble(b[0]);
73         lo = qnap_tsx09_parse_hex_nibble(b[1]);
74
75         if (hi < 0 || lo < 0)
76                 return -1;
77
78         return (hi << 4) | lo;
79 }
80
81 static int __init qnap_tsx09_check_mac_addr(const char *addr_str)
82 {
83         u_int8_t addr[6];
84         int i;
85
86         for (i = 0; i < 6; i++) {
87                 int byte;
88
89                 /*
90                  * Enforce "xx:xx:xx:xx:xx:xx\n" format.
91                  */
92                 if (addr_str[(i * 3) + 2] != ((i < 5) ? ':' : '\n'))
93                         return -1;
94
95                 byte = qnap_tsx09_parse_hex_byte(addr_str + (i * 3));
96                 if (byte < 0)
97                         return -1;
98                 addr[i] = byte;
99         }
100
101         printk(KERN_INFO "tsx09: found ethernet mac address ");
102         for (i = 0; i < 6; i++)
103                 printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
104
105         memcpy(qnap_tsx09_eth_data.mac_addr, addr, 6);
106
107         return 0;
108 }
109
110 /*
111  * The 'NAS Config' flash partition has an ext2 filesystem which
112  * contains a file that has the ethernet MAC address in plain text
113  * (format "xx:xx:xx:xx:xx:xx\n").
114  */
115 void __init qnap_tsx09_find_mac_addr(u32 mem_base, u32 size)
116 {
117         unsigned long addr;
118
119         for (addr = mem_base; addr < (mem_base + size); addr += 1024) {
120                 char *nor_page;
121                 int ret = 0;
122
123                 nor_page = ioremap(addr, 1024);
124                 if (nor_page != NULL) {
125                         ret = qnap_tsx09_check_mac_addr(nor_page);
126                         iounmap(nor_page);
127                 }
128
129                 if (ret == 0)
130                         break;
131         }
132 }