summaryrefstree
path: root/kernel/drivers/pci/host/pci-exynos8890_cal.c
blob: a565a01c62c4284a8ab03e2dd0bf8fb7fbc3b0fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * PCIe phy driver for Samsung EXYNOS8890
 *
 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * Author: Kyoungil Kim <ki0351.kim@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/of_gpio.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/exynos-pci-noti.h>
#include "pcie-designware.h"
#include "pci-exynos.h"

#if IS_ENABLED(CONFIG_EXYNOS_OTP)
#include <linux/exynos_otp.h>
#endif
static struct exynos_pcie g_pcie[MAX_RC_NUM];

/* avoid checking rx elecidle when access DBI */
void exynos_phy_check_rx_elecidle(void *phy_pcs_base_regs, int val)
{
	u32 reg_val;

	reg_val = readl(phy_pcs_base_regs + 0xEC);
	reg_val &= ~(0x1 << 3);
	reg_val |= (val << 3);
	writel(reg_val, phy_pcs_base_regs + 0xEC);
}
/* PHY all power down */
void exynos_phy_all_pwrdn(void *phy_base_regs, void *sysreg_base_regs, int ch_num)
{
	u32 val;

	val = readl(phy_base_regs + (0x15 * 4));
	val |= 0xf << 3;
	writel(val, phy_base_regs + (0x15 * 4));
	writel(0xff, phy_base_regs + (0x4E * 4));
	writel(0x3f, phy_base_regs + (0x4F * 4));
}

/* PHY all power down clear */
void exynos_phy_all_pwrdn_clear(void *phy_base_regs, void *sysreg_base_regs, int ch_num)
{
	u32 val;

	val = readl(phy_base_regs + (0x15 * 4));
	val &= ~(0xf << 3);
	writel(val, phy_base_regs + (0x15 * 4));
	writel(0x0, phy_base_regs + (0x4E * 4));
	writel(0x0, phy_base_regs + (0x4F * 4));
}

void exynos_pcie_phy_otp_config(void *phy_base_regs, int ch_num)
{
#if IS_ENABLED(CONFIG_EXYNOS_OTP)
	u8 utype;
	u8 uindex_count;
	struct tune_bits *data;
	u32 index;
	u32 value;
	u16 magic_code;
	u32 i;

	struct pcie_port *pp = &g_pcie[ch_num].pp;

	if (ch_num == 0)
		magic_code = 0x5030;
	else if (ch_num == 1)
		magic_code = 0x5031;
	else
		return;

	if (otp_tune_bits_parsed(magic_code, &utype, &uindex_count, &data) == 0) {
		dev_err(pp->dev, "%s: [OTP] uindex_count %d", __func__, uindex_count);
		for (i = 0; i < uindex_count; i++){
			index = data[i].index;
			value = data[i].value;

			dev_err(pp->dev, "%s: [OTP][Return Value] index = 0x%x, value = 0x%x\n", __func__, index, value);
			dev_err(pp->dev, "%s: [OTP][Before Reg Value] offset 0x%x = 0x%x\n", __func__, index * 4, readl(phy_base_regs + (index * 4)));
			if(readl(phy_base_regs + (index * 4)) != value)
				writel(value, phy_base_regs + (index * 4));
			else
				return;
			dev_err(pp->dev, "%s: [OTP][After Reg Value] offset 0x%x = 0x%x\n", __func__, index * 4, readl(phy_base_regs + (index * 4)));
		}
	}
#else
	return;
#endif
}

void exynos_pcie_phy_config(void *phy_base_regs, void *phy_pcs_base_regs, void *sysreg_base_regs, void *elbi_base_regs, int ch_num)
{
	/* 26MHz gen1 */
	u32 cmn_config_val[26] = {0x01, 0x0F, 0xA6, 0x31, 0x90, 0x62, 0x20, 0x00, 0x00, 0xA7, 0x0A,
				  0x37, 0x20, 0x08, 0xEF, 0xFC, 0x96, 0x14, 0x00, 0x10, 0x60, 0x01,
				  0x00, 0x00, 0x04, 0x10};
	u32 trsv_config_val[41] = {0x31, 0xF4, 0xF4, 0x80, 0x25, 0x40, 0xD8, 0x03, 0x35, 0x55, 0x4C,
				   0xC3, 0x10, 0x54, 0x70, 0xC5, 0x00, 0x2F, 0x38, 0xA4, 0x00, 0x3B,
				   0x30, 0x9A, 0x64, 0x00, 0x1F, 0x83, 0x1B, 0x01, 0xE0, 0x00, 0x00,
				   0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1D, 0x00};
	int i;

	writel(readl(sysreg_base_regs) & ~(0x1 << 1), sysreg_base_regs);
	writel((((readl(sysreg_base_regs + 0xC) & ~(0xf << 4)) & ~(0xf << 2)) | (0x3 << 2)) & ~(0x1 << 1), sysreg_base_regs + 0xC);

	/* pcs_g_rst */
	writel(0x1, elbi_base_regs + 0x288);
	udelay(10);
	writel(0x0, elbi_base_regs + 0x288);
	udelay(10);
	writel(0x1, elbi_base_regs + 0x288);
	udelay(10);

	/* PHY Common block Setting */
	for (i = 0; i < 26; i++)
		writel(cmn_config_val[i], phy_base_regs + (i * 4));

	/* PHY Tranceiver/Receiver block Setting */
	for (i = 0; i < 41; i++)
		writel(trsv_config_val[i], phy_base_regs + ((0x30 + i) * 4));

#if IS_ENABLED(CONFIG_EXYNOS_OTP)
	/* PHY OTP Tuning bit configuration Setting */
	exynos_pcie_phy_otp_config(phy_base_regs, ch_num);
#endif

	/* tx amplitude control */
	writel(0x14, phy_base_regs + (0x5C * 4));

	/* tx latency */
	writel(0x70, phy_pcs_base_regs + 0xF8);

	/* PRGM_TIMEOUT_L1SS_VAL Setting */
	writel(readl(phy_pcs_base_regs + 0xC) | (0x1 << 4), phy_pcs_base_regs + 0xC);

	/* PCIE_MAC CMN_RST */
	writel(0x1, elbi_base_regs + 0x290);
	udelay(10);
	writel(0x0, elbi_base_regs + 0x290);
	udelay(10);
	writel(0x1, elbi_base_regs + 0x290);
	udelay(10);

	/* PCIE_PHY PCS&PMA(CMN)_RST */
	writel(0x1, elbi_base_regs + 0x28C);
	udelay(10);
	writel(0x0, elbi_base_regs + 0x28C);
	udelay(10);
	writel(0x1, elbi_base_regs + 0x28C);
	udelay(10);
}

void exynos_pcie_phy_init(struct pcie_port *pp)
{
	struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);

	dev_info(pp->dev, "Initialize PHY functions.\n");

	exynos_pcie->phy_ops.phy_check_rx_elecidle =
		exynos_phy_check_rx_elecidle;
	exynos_pcie->phy_ops.phy_all_pwrdn = exynos_phy_all_pwrdn;
	exynos_pcie->phy_ops.phy_all_pwrdn_clear = exynos_phy_all_pwrdn_clear;
	exynos_pcie->phy_ops.phy_config = exynos_pcie_phy_config;
}