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/timex.h>
17 #include <linux/serial_reg.h>
18 #include "tsx09-common.h"
19 
20 /*****************************************************************************
21  * QNAP TS-x09 specific power off method via UART1-attached PIC
22  ****************************************************************************/
23 
24 #define UART1_REG(x)	(UART1_VIRT_BASE + ((UART_##x) << 2))
25 
26 void qnap_tsx09_power_off(void)
27 {
28 	/* 19200 baud divisor */
29 	const unsigned divisor = ((ORION5X_TCLK + (8 * 19200)) / (16 * 19200));
30 
31 	pr_info("%s: triggering power-off...\n", __func__);
32 
33 	/* hijack uart1 and reset into sane state (19200,8n1) */
34 	writel(0x83, UART1_REG(LCR));
35 	writel(divisor & 0xff, UART1_REG(DLL));
36 	writel((divisor >> 8) & 0xff, UART1_REG(DLM));
37 	writel(0x03, UART1_REG(LCR));
38 	writel(0x00, UART1_REG(IER));
39 	writel(0x00, UART1_REG(FCR));
40 	writel(0x00, UART1_REG(MCR));
41 
42 	/* send the power-off command 'A' to PIC */
43 	writel('A', UART1_REG(TX));
44 }
45 
46 /*****************************************************************************
47  * Ethernet
48  ****************************************************************************/
49 
50 struct mv643xx_eth_platform_data qnap_tsx09_eth_data = {
51 	.phy_addr	= 8,
52 };
53 
54 static int __init qnap_tsx09_parse_hex_nibble(char n)
55 {
56 	if (n >= '0' && n <= '9')
57 		return n - '0';
58 
59 	if (n >= 'A' && n <= 'F')
60 		return n - 'A' + 10;
61 
62 	if (n >= 'a' && n <= 'f')
63 		return n - 'a' + 10;
64 
65 	return -1;
66 }
67 
68 static int __init qnap_tsx09_parse_hex_byte(const char *b)
69 {
70 	int hi;
71 	int lo;
72 
73 	hi = qnap_tsx09_parse_hex_nibble(b[0]);
74 	lo = qnap_tsx09_parse_hex_nibble(b[1]);
75 
76 	if (hi < 0 || lo < 0)
77 		return -1;
78 
79 	return (hi << 4) | lo;
80 }
81 
82 static int __init qnap_tsx09_check_mac_addr(const char *addr_str)
83 {
84 	u_int8_t addr[6];
85 	int i;
86 
87 	for (i = 0; i < 6; i++) {
88 		int byte;
89 
90 		/*
91 		 * Enforce "xx:xx:xx:xx:xx:xx\n" format.
92 		 */
93 		if (addr_str[(i * 3) + 2] != ((i < 5) ? ':' : '\n'))
94 			return -1;
95 
96 		byte = qnap_tsx09_parse_hex_byte(addr_str + (i * 3));
97 		if (byte < 0)
98 			return -1;
99 		addr[i] = byte;
100 	}
101 
102 	printk(KERN_INFO "tsx09: found ethernet mac address ");
103 	for (i = 0; i < 6; i++)
104 		printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
105 
106 	memcpy(qnap_tsx09_eth_data.mac_addr, addr, 6);
107 
108 	return 0;
109 }
110 
111 /*
112  * The 'NAS Config' flash partition has an ext2 filesystem which
113  * contains a file that has the ethernet MAC address in plain text
114  * (format "xx:xx:xx:xx:xx:xx\n").
115  */
116 void __init qnap_tsx09_find_mac_addr(u32 mem_base, u32 size)
117 {
118 	unsigned long addr;
119 
120 	for (addr = mem_base; addr < (mem_base + size); addr += 1024) {
121 		char *nor_page;
122 		int ret = 0;
123 
124 		nor_page = ioremap(addr, 1024);
125 		if (nor_page != NULL) {
126 			ret = qnap_tsx09_check_mac_addr(nor_page);
127 			iounmap(nor_page);
128 		}
129 
130 		if (ret == 0)
131 			break;
132 	}
133 }
134