1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2530c854aSSylver Bruneau /*
3530c854aSSylver Bruneau * QNAP TS-x09 Boards common functions
4530c854aSSylver Bruneau *
5530c854aSSylver Bruneau * Maintainers: Lennert Buytenhek <buytenh@marvell.com>
6530c854aSSylver Bruneau * Byron Bradley <byron.bbradley@gmail.com>
7530c854aSSylver Bruneau */
8530c854aSSylver Bruneau
9530c854aSSylver Bruneau #include <linux/kernel.h>
10530c854aSSylver Bruneau #include <linux/pci.h>
11530c854aSSylver Bruneau #include <linux/mv643xx_eth.h>
12a10b188fSSylver Bruneau #include <linux/timex.h>
13530c854aSSylver Bruneau #include <linux/serial_reg.h>
14c22c2c60SArnd Bergmann #include "orion5x.h"
15530c854aSSylver Bruneau #include "tsx09-common.h"
16ebe35affSLennert Buytenhek #include "common.h"
17530c854aSSylver Bruneau
18530c854aSSylver Bruneau /*****************************************************************************
19530c854aSSylver Bruneau * QNAP TS-x09 specific power off method via UART1-attached PIC
20530c854aSSylver Bruneau ****************************************************************************/
21530c854aSSylver Bruneau
22530c854aSSylver Bruneau #define UART1_REG(x) (UART1_VIRT_BASE + ((UART_##x) << 2))
23530c854aSSylver Bruneau
qnap_tsx09_power_off(void)24530c854aSSylver Bruneau void qnap_tsx09_power_off(void)
25530c854aSSylver Bruneau {
26530c854aSSylver Bruneau /* 19200 baud divisor */
27ebe35affSLennert Buytenhek const unsigned divisor = ((orion5x_tclk + (8 * 19200)) / (16 * 19200));
28530c854aSSylver Bruneau
29530c854aSSylver Bruneau pr_info("%s: triggering power-off...\n", __func__);
30530c854aSSylver Bruneau
31530c854aSSylver Bruneau /* hijack uart1 and reset into sane state (19200,8n1) */
32530c854aSSylver Bruneau writel(0x83, UART1_REG(LCR));
33530c854aSSylver Bruneau writel(divisor & 0xff, UART1_REG(DLL));
34530c854aSSylver Bruneau writel((divisor >> 8) & 0xff, UART1_REG(DLM));
35530c854aSSylver Bruneau writel(0x03, UART1_REG(LCR));
36530c854aSSylver Bruneau writel(0x00, UART1_REG(IER));
37530c854aSSylver Bruneau writel(0x00, UART1_REG(FCR));
38530c854aSSylver Bruneau writel(0x00, UART1_REG(MCR));
39530c854aSSylver Bruneau
40530c854aSSylver Bruneau /* send the power-off command 'A' to PIC */
41530c854aSSylver Bruneau writel('A', UART1_REG(TX));
42530c854aSSylver Bruneau }
43530c854aSSylver Bruneau
44530c854aSSylver Bruneau /*****************************************************************************
45530c854aSSylver Bruneau * Ethernet
46530c854aSSylver Bruneau ****************************************************************************/
47530c854aSSylver Bruneau
48530c854aSSylver Bruneau struct mv643xx_eth_platform_data qnap_tsx09_eth_data = {
49ac840605SLennert Buytenhek .phy_addr = MV643XX_ETH_PHY_ADDR(8),
50530c854aSSylver Bruneau };
51530c854aSSylver Bruneau
qnap_tsx09_parse_hex_nibble(char n)5213a55372SDavid S. Miller static int __init qnap_tsx09_parse_hex_nibble(char n)
5313a55372SDavid S. Miller {
5413a55372SDavid S. Miller if (n >= '0' && n <= '9')
5513a55372SDavid S. Miller return n - '0';
5613a55372SDavid S. Miller
5713a55372SDavid S. Miller if (n >= 'A' && n <= 'F')
5813a55372SDavid S. Miller return n - 'A' + 10;
5913a55372SDavid S. Miller
6013a55372SDavid S. Miller if (n >= 'a' && n <= 'f')
6113a55372SDavid S. Miller return n - 'a' + 10;
6213a55372SDavid S. Miller
6313a55372SDavid S. Miller return -1;
6413a55372SDavid S. Miller }
6513a55372SDavid S. Miller
qnap_tsx09_parse_hex_byte(const char * b)6613a55372SDavid S. Miller static int __init qnap_tsx09_parse_hex_byte(const char *b)
6713a55372SDavid S. Miller {
6813a55372SDavid S. Miller int hi;
6913a55372SDavid S. Miller int lo;
7013a55372SDavid S. Miller
7113a55372SDavid S. Miller hi = qnap_tsx09_parse_hex_nibble(b[0]);
7213a55372SDavid S. Miller lo = qnap_tsx09_parse_hex_nibble(b[1]);
7313a55372SDavid S. Miller
7413a55372SDavid S. Miller if (hi < 0 || lo < 0)
7513a55372SDavid S. Miller return -1;
7613a55372SDavid S. Miller
7713a55372SDavid S. Miller return (hi << 4) | lo;
7813a55372SDavid S. Miller }
7913a55372SDavid S. Miller
qnap_tsx09_check_mac_addr(const char * addr_str)80530c854aSSylver Bruneau static int __init qnap_tsx09_check_mac_addr(const char *addr_str)
81530c854aSSylver Bruneau {
82530c854aSSylver Bruneau u_int8_t addr[6];
8313a55372SDavid S. Miller int i;
84530c854aSSylver Bruneau
8513a55372SDavid S. Miller for (i = 0; i < 6; i++) {
8613a55372SDavid S. Miller int byte;
8713a55372SDavid S. Miller
8813a55372SDavid S. Miller /*
8913a55372SDavid S. Miller * Enforce "xx:xx:xx:xx:xx:xx\n" format.
9013a55372SDavid S. Miller */
9113a55372SDavid S. Miller if (addr_str[(i * 3) + 2] != ((i < 5) ? ':' : '\n'))
92530c854aSSylver Bruneau return -1;
93530c854aSSylver Bruneau
9413a55372SDavid S. Miller byte = qnap_tsx09_parse_hex_byte(addr_str + (i * 3));
9513a55372SDavid S. Miller if (byte < 0)
9613a55372SDavid S. Miller return -1;
9713a55372SDavid S. Miller addr[i] = byte;
9813a55372SDavid S. Miller }
9913a55372SDavid S. Miller
1007a1436d5SJoe Perches printk(KERN_INFO "tsx09: found ethernet mac address %pM\n", addr);
101530c854aSSylver Bruneau
102530c854aSSylver Bruneau memcpy(qnap_tsx09_eth_data.mac_addr, addr, 6);
103530c854aSSylver Bruneau
104530c854aSSylver Bruneau return 0;
105530c854aSSylver Bruneau }
106530c854aSSylver Bruneau
107530c854aSSylver Bruneau /*
108530c854aSSylver Bruneau * The 'NAS Config' flash partition has an ext2 filesystem which
109530c854aSSylver Bruneau * contains a file that has the ethernet MAC address in plain text
110530c854aSSylver Bruneau * (format "xx:xx:xx:xx:xx:xx\n").
111530c854aSSylver Bruneau */
qnap_tsx09_find_mac_addr(u32 mem_base,u32 size)112530c854aSSylver Bruneau void __init qnap_tsx09_find_mac_addr(u32 mem_base, u32 size)
113530c854aSSylver Bruneau {
114530c854aSSylver Bruneau unsigned long addr;
115530c854aSSylver Bruneau
116530c854aSSylver Bruneau for (addr = mem_base; addr < (mem_base + size); addr += 1024) {
11713a55372SDavid S. Miller char *nor_page;
118530c854aSSylver Bruneau int ret = 0;
119530c854aSSylver Bruneau
120530c854aSSylver Bruneau nor_page = ioremap(addr, 1024);
121530c854aSSylver Bruneau if (nor_page != NULL) {
12213a55372SDavid S. Miller ret = qnap_tsx09_check_mac_addr(nor_page);
123530c854aSSylver Bruneau iounmap(nor_page);
124530c854aSSylver Bruneau }
125530c854aSSylver Bruneau
126530c854aSSylver Bruneau if (ret == 0)
127530c854aSSylver Bruneau break;
128530c854aSSylver Bruneau }
129530c854aSSylver Bruneau }
130