blob: 98e0dfa518510e335c6fd32aaba2fe685c367f58 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002 Wolfgang Grandegger <wg@denx.de>
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00005 */
6
7#include <common.h>
8#include <mpc824x.h>
9#include <asm/io.h>
10#include <pci.h>
11
12#include "pn62.h"
13
14typedef struct {
15 pci_dev_t devno;
16 volatile u32 *csr;
17
18} i2155x_t;
19
20static i2155x_t i2155x = { 0, NULL };
21
22static struct pci_device_id i2155x_ids[] = {
23 { 0x1011, 0x0046 }, /* i21554 */
24 { 0x8086, 0xb555 } /* i21555 */
25};
26
27int i2155x_init(void)
28{
29 pci_dev_t devno;
30 u32 val;
31 int i;
32
33 /*
34 * Find the Intel bridge.
35 */
36 if ((devno = pci_find_devices(i2155x_ids, 0)) < 0) {
37 printf("Error: Intel bridge 2155x not found!\n");
38 return -1;
39 }
40 i2155x.devno = devno;
41
42 /*
43 * Get auto-configured base address for CSR access.
44 */
45 pci_read_config_dword(devno, PCI_BASE_ADDRESS_1, &val);
46 if (val & PCI_BASE_ADDRESS_SPACE_IO) {
47 val &= PCI_BASE_ADDRESS_IO_MASK;
48 i2155x.csr = (volatile u32 *)(_IO_BASE + val);
49 } else {
50 val &= PCI_BASE_ADDRESS_MEM_MASK;
51 i2155x.csr = (volatile u32 *)val;
52 }
53
54 /*
55 * Translate downstream memory 2 (bar3) to base of shared memory.
56 */
57 i2155x_set_bar_base(3, PN62_SMEM_DEFAULT);
58
59 /*
60 * Enable memory space, I/O space and bus master bits
61 * in both Primary and Secondary command registers.
62 */
63 val = PCI_COMMAND_MEMORY|PCI_COMMAND_MASTER|PCI_COMMAND_IO;
64 pci_write_config_word(devno, 0x44, val);
65 pci_write_config_word(devno, 0x04, val);
66
67 /*
68 * Clear scratchpad registers.
69 */
70 for (i = 0; i < (I2155X_SCRAPAD_MAX - 1); i++) {
71 i2155x_write_scrapad(i, 0x0);
72 }
73
74 /*
75 * Set interrupt line for Linux.
76 */
77 pci_write_config_byte(devno, PCI_INTERRUPT_LINE, 3);
78
79 return 0;
80}
81
82/*
83 * Access the Scratchpad registers 0..7 of the Intel bridge.
84 */
85void i2155x_write_scrapad(int idx, u32 val)
86{
87 if (idx >= 0 && idx < I2155X_SCRAPAD_MAX)
88 out_le32(i2155x.csr + (I2155X_SCRAPAD_ADDR/4) + idx, val);
89 else
90 printf("i2155x_write_scrapad: invalid index\n");
91}
92
93u32 i2155x_read_scrapad(int idx)
94{
95 if (idx >= 0 && idx < I2155X_SCRAPAD_MAX)
96 return in_le32(i2155x.csr + (I2155X_SCRAPAD_ADDR/4) + idx);
97 else
98 printf("i2155x_read_scrapad: invalid index\n");
99 return -1;
100}
101
102void i2155x_set_bar_base(int bar, u32 base)
103{
104 if (bar >= 2 && bar <= 4) {
105 pci_write_config_dword(i2155x.devno,
106 I2155X_BAR2_BASE + (bar - 2) * 4,
107 base);
108 }
109}
110
111/*
112 * Read Vital Product Data (VPD) from the Serial EPROM attached
113 * to the Intel bridge.
114 */
115int i2155x_read_vpd(int offset, int size, unsigned char *data)
116{
117 int i, n;
118 u16 val16;
119
120 for (i = 0; i < size; i++) {
121 pci_write_config_word(i2155x.devno, I2155X_VPD_ADDR,
122 offset + i - I2155X_VPD_START);
123 for (n = 10000; n > 0; n--) {
124 pci_read_config_word(i2155x.devno, I2155X_VPD_ADDR, &val16);
125 if ((val16 & 0x8000) != 0) /* wait for completion */
126 break;
127 udelay(100);
128 }
129 if (n == 0) {
130 printf("i2155x_read_vpd: TIMEOUT\n");
131 return -1;
132 }
133
134 pci_read_config_byte(i2155x.devno, I2155X_VPD_DATA, &data[i]);
135 }
136
137 return i;
138}
139
140static struct pci_device_id am79c95x_ids [] = {
wdenk8bde7f72003-06-27 21:31:46 +0000141 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE },
142 { }
wdenkc6097192002-11-03 00:24:07 +0000143};
144
145
146/*
147 * Initialize the AMD ethernet controllers.
148 */
149int am79c95x_init(void)
150{
151 pci_dev_t devno;
152 int i;
153
154 /*
155 * Set interrupt line for Linux.
156 */
157 for (i = 0; i < 2; i++) {
158 if ((devno = pci_find_devices(am79c95x_ids, i)) < 0)
159 break;
160 pci_write_config_byte(devno, PCI_INTERRUPT_LINE, 2+i);
161 }
162 if (i < 2)
163 printf("Error: Only %d AMD Ethernet Controller found!\n", i);
164
165 return 0;
166}
167
168
169void set_led(unsigned int number, unsigned int function)
170{
171 volatile u8 *addr;
172
173 if ((number >= 0) && (number < PN62_LED_MAX) &&
174 (function >= 0) && (function <= LED_LAST_FUNCTION)) {
175 addr = (volatile u8 *)(PN62_LED_BASE + number * 8);
176 out_8(addr, function&0xff);
177 }
178}
179
180/*
181 * Show fatal error indicated by Kinght Rider(tm) effect
182 * in LEDS 0-7. LEDS 8-11 contain 4 bit error code.
183 * Note: this function will not terminate.
184 */
185void fatal_error(unsigned int error_code)
186{
187 int i, d;
188
189 for (i = 0; i < 12; i++) {
190 set_led(i, LED_0);
191 }
192
193 /*
194 * Write error code.
195 */
196 set_led(8, (error_code & 0x01) ? LED_1 : LED_0);
197 set_led(9, (error_code & 0x02) ? LED_1 : LED_0);
198 set_led(10, (error_code & 0x04) ? LED_1 : LED_0);
199 set_led(11, (error_code & 0x08) ? LED_1 : LED_0);
200
201 /*
202 * Yay - Knight Rider effect!
203 */
204 while(1) {
205 unsigned int delay = 2000;
206
207 for (i = 0; i < 8; i++) {
208 set_led(i, LED_1);
209 for (d = 0; d < delay; d++);
210 set_led(i, LED_0);
211 }
212
213 for (i = 7; i > 0; i--) {
214 set_led(i, LED_1);
215 for (d = 0; d < delay; d++);
216 set_led(i, LED_0);
217 }
218 }
219}