blob: 62017360e74861c3513b8d147447270ef2b527a9 [file] [log] [blame]
Morgan Tsai1602dd52007-10-29 21:00:14 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Morgan Tsai1602dd52007-10-29 21:00:14 +00003 *
4 * Copyright (C) 2004 Tyan Computer
5 * Written by Yinghai Lu <yhlu@tyan.com> for Tyan Computer.
6 * Copyright (C) 2006,2007 AMD
7 * Written by Yinghai Lu <yinghai.lu@amd.com> for AMD.
8 * Copyright (C) 2007 Silicon Integrated Systems Corp. (SiS)
9 * Written by Morgan Tsai <my_tsai@sis.com> for SiS.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <console/console.h>
27#include <device/device.h>
28#include <device/smbus.h>
29#include <device/pci.h>
30#include <device/pci_ids.h>
31#include <device/pci_ops.h>
32#include <arch/io.h>
33#include <delay.h>
34#include "sis966.h"
35
36
37uint8_t SiS_SiS191_init[6][3]={
Morgan Tsai218c2652007-11-02 16:09:58 +000038{0x04, 0xFF, 0x07},
39{0x2C, 0xFF, 0x39},
40{0x2D, 0xFF, 0x10},
41{0x2E, 0xFF, 0x91},
Morgan Tsai1602dd52007-10-29 21:00:14 +000042{0x2F, 0xFF, 0x01},
43{0x00, 0x00, 0x00} //End of table
44};
45
Morgan Tsai218c2652007-11-02 16:09:58 +000046
47#define StatusReg 0x1
Morgan Tsai1602dd52007-10-29 21:00:14 +000048#define SMI_READ 0x0
49#define SMI_REQUEST 0x10
50#define TRUE 1
51#define FALSE 0
52
53uint16_t MacAddr[3];
54
55
56void writeApcByte(int addr, uint8_t value)
57{
58 outb(addr,0x78);
Morgan Tsai218c2652007-11-02 16:09:58 +000059 outb(value,0x79);
Morgan Tsai1602dd52007-10-29 21:00:14 +000060}
61uint8_t readApcByte(int addr)
62{
63 uint8_t value;
64 outb(addr,0x78);
65 value=inb(0x79);
66 return(value);
67}
68
69static void readApcMacAddr(void)
70{
71 uint8_t i;
72
73// enable APC in south bridge sis966 D2F0
74
Morgan Tsai218c2652007-11-02 16:09:58 +000075 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +000076 outl((inl(0xcfc) & 0xfffffffd),0xcfc ); // enable IO78/79h for APC Index/Data
77
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000078 printk(BIOS_DEBUG, "MAC addr in APC = ");
Morgan Tsai218c2652007-11-02 16:09:58 +000079 for(i = 0x9 ; i <=0xe ; i++)
Morgan Tsai1602dd52007-10-29 21:00:14 +000080 {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000081 printk(BIOS_DEBUG, "%2.2x",readApcByte(i));
Morgan Tsai1602dd52007-10-29 21:00:14 +000082 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000083 printk(BIOS_DEBUG, "\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +000084
85 /* Set APC Reload */
86 writeApcByte(0x7,readApcByte(0x7)&0xf7);
87 writeApcByte(0x7,readApcByte(0x7)|0x0a);
Morgan Tsai218c2652007-11-02 16:09:58 +000088
Morgan Tsai1602dd52007-10-29 21:00:14 +000089 /* disable APC in south bridge */
Morgan Tsai218c2652007-11-02 16:09:58 +000090 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +000091 outl(inl(0xcfc)&0xffffffbf,0xcfc);
92}
93
94static void set_apc(struct device *dev)
95{
Morgan Tsai1602dd52007-10-29 21:00:14 +000096 uint16_t addr;
Morgan Tsai1602dd52007-10-29 21:00:14 +000097 uint16_t i;
98 uint8_t bTmp;
99
100 /* enable APC in south bridge sis966 D2F0 */
Morgan Tsai218c2652007-11-02 16:09:58 +0000101 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000102 outl((inl(0xcfc) & 0xfffffffd),0xcfc ); // enable IO78/79h for APC Index/Data
Morgan Tsai218c2652007-11-02 16:09:58 +0000103
Morgan Tsai1602dd52007-10-29 21:00:14 +0000104 for(i = 0 ; i <3; i++)
105 {
106 addr=0x9+2*i;
107 writeApcByte(addr,(uint8_t)(MacAddr[i]&0xFF));
108 writeApcByte(addr+1L,(uint8_t)((MacAddr[i]>>8)&0xFF));
109 // printf("%x - ",readMacAddrByte(0x59+i));
110 }
111
112 /* Set APC Reload */
113 writeApcByte(0x7,readApcByte(0x7)&0xf7);
114 writeApcByte(0x7,readApcByte(0x7)|0x0a);
Morgan Tsai218c2652007-11-02 16:09:58 +0000115
Morgan Tsai1602dd52007-10-29 21:00:14 +0000116 /* disable APC in south bridge */
Morgan Tsai218c2652007-11-02 16:09:58 +0000117 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000118 outl(inl(0xcfc)&0xffffffbf,0xcfc);
119
Morgan Tsai218c2652007-11-02 16:09:58 +0000120 // CFG reg0x73 bit=1, tell driver MAC Address load to APC
121 bTmp = pci_read_config8(dev, 0x73);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000122 bTmp|=0x1;
123 pci_write_config8(dev, 0x73, bTmp);
124}
125
126//-----------------------------------------------------------------------------
127// Procedure: ReadEEprom
128//
129// Description: This routine serially reads one word out of the EEPROM.
130//
131// Arguments:
132// Reg - EEPROM word to read.
133//
134// Returns:
135// Contents of EEPROM word (Reg).
136//-----------------------------------------------------------------------------
137#define LoopNum 200
138static unsigned long ReadEEprom( struct device *dev, uint32_t base, uint32_t Reg)
139{
Carl-Daniel Hailfingera3588922007-11-05 22:21:27 +0000140 uint32_t data;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000141 uint32_t i;
142 uint32_t ulValue;
Morgan Tsai218c2652007-11-02 16:09:58 +0000143
144
Morgan Tsai1602dd52007-10-29 21:00:14 +0000145 ulValue = (0x80 | (0x2 << 8) | (Reg << 10)); //BIT_7
146
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000147 write32(base+0x3c, ulValue);
Morgan Tsai218c2652007-11-02 16:09:58 +0000148
Morgan Tsai1602dd52007-10-29 21:00:14 +0000149 mdelay(10);
150
151 for(i=0 ; i <= LoopNum; i++)
152 {
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000153 ulValue=read32(base+0x3c);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000154
155 if(!(ulValue & 0x0080)) //BIT_7
156 break;
157
158 mdelay(100);
159 }
Morgan Tsai218c2652007-11-02 16:09:58 +0000160
Morgan Tsai1602dd52007-10-29 21:00:14 +0000161 mdelay(50);
162
163 if(i==LoopNum) data=0x10000;
164 else{
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000165 ulValue=read32(base+0x3c);
Carl-Daniel Hailfingera3588922007-11-05 22:21:27 +0000166 data = ((ulValue & 0xffff0000) >> 16);
Morgan Tsai218c2652007-11-02 16:09:58 +0000167 }
168
Morgan Tsai1602dd52007-10-29 21:00:14 +0000169 return data;
170}
171
172static int phy_read(uint32_t base, unsigned phy_addr, unsigned phy_reg)
173{
174 uint32_t ulValue;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000175 uint32_t Read_Cmd;
176 uint16_t usData;
177
Morgan Tsai1602dd52007-10-29 21:00:14 +0000178
Morgan Tsai218c2652007-11-02 16:09:58 +0000179
180 Read_Cmd = ((phy_reg << 11) |
Morgan Tsai1602dd52007-10-29 21:00:14 +0000181 (phy_addr << 6) |
182 SMI_READ |
183 SMI_REQUEST);
184
185 // SmiMgtInterface Reg is the SMI management interface register(offset 44h) of MAC
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000186 write32(base+0x44, Read_Cmd);
Morgan Tsai218c2652007-11-02 16:09:58 +0000187
Morgan Tsai1602dd52007-10-29 21:00:14 +0000188 // Polling SMI_REQ bit to be deasserted indicated read command completed
189 do
190 {
191 // Wait 20 usec before checking status
Morgan Tsai1602dd52007-10-29 21:00:14 +0000192 mdelay(20);
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000193 ulValue = read32(base+0x44);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000194 } while((ulValue & SMI_REQUEST) != 0);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000195 //printk(BIOS_DEBUG, "base %x cmd %lx ret val %lx\n", tmp,Read_Cmd,ulValue);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000196 usData=(ulValue>>16);
197
Morgan Tsai218c2652007-11-02 16:09:58 +0000198
Morgan Tsai1602dd52007-10-29 21:00:14 +0000199
200 return usData;
201
202}
203
204// Detect a valid PHY
205// If there exist a valid PHY then return TRUE, else return FALSE
206static int phy_detect(uint32_t base,uint16_t *PhyAddr) //BOOL PHY_Detect()
207{
208 int bFoundPhy = FALSE;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000209 uint16_t usData;
210 int PhyAddress = 0;
Morgan Tsai218c2652007-11-02 16:09:58 +0000211
212
Morgan Tsai1602dd52007-10-29 21:00:14 +0000213 // Scan all PHY address(0 ~ 31) to find a valid PHY
214 for(PhyAddress = 0; PhyAddress < 32; PhyAddress++)
Morgan Tsai218c2652007-11-02 16:09:58 +0000215 {
216 usData=phy_read(base,PhyAddress,StatusReg); // Status register is a PHY's register(offset 01h)
217
218 // Found a valid PHY
219
Morgan Tsai1602dd52007-10-29 21:00:14 +0000220 if((usData != 0x0) && (usData != 0xffff))
221 {
222 bFoundPhy = TRUE;
223 break;
224 }
225 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000226
Morgan Tsai1602dd52007-10-29 21:00:14 +0000227
228 if(!bFoundPhy)
229 {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000230 printk(BIOS_DEBUG, "PHY not found !!!! \n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000231 }
232
233 *PhyAddr=PhyAddress;
Morgan Tsai218c2652007-11-02 16:09:58 +0000234
Morgan Tsai1602dd52007-10-29 21:00:14 +0000235 return bFoundPhy;
236}
237
238
239static void nic_init(struct device *dev)
240{
Morgan Tsai218c2652007-11-02 16:09:58 +0000241 int val;
242 uint16_t PhyAddr;
Morgan Tsai218c2652007-11-02 16:09:58 +0000243 uint32_t base;
244 struct resource *res;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000245
Morgan Tsai1602dd52007-10-29 21:00:14 +0000246
Morgan Tsai218c2652007-11-02 16:09:58 +0000247 print_debug("NIC_INIT:---------->\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000248
249
250//-------------- enable NIC (SiS19x) -------------------------
251{
252 uint8_t temp8;
253 int i=0;
254 while(SiS_SiS191_init[i][0] != 0)
Morgan Tsai218c2652007-11-02 16:09:58 +0000255 {
256 temp8 = pci_read_config8(dev, SiS_SiS191_init[i][0]);
257 temp8 &= SiS_SiS191_init[i][1];
258 temp8 |= SiS_SiS191_init[i][2];
259 pci_write_config8(dev, SiS_SiS191_init[i][0], temp8);
260 i++;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000261 };
262}
263//-----------------------------------------------------------
264
Morgan Tsai1602dd52007-10-29 21:00:14 +0000265{
Morgan Tsai218c2652007-11-02 16:09:58 +0000266 unsigned long i;
267 unsigned long ulValue;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000268
Morgan Tsai1602dd52007-10-29 21:00:14 +0000269 res = find_resource(dev, 0x10);
270
Morgan Tsai218c2652007-11-02 16:09:58 +0000271 if(!res)
272 {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000273 printk(BIOS_DEBUG, "NIC Cannot find resource..\r\n");
Morgan Tsai218c2652007-11-02 16:09:58 +0000274 return;
275 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000276 base = res->base;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000277 printk(BIOS_DEBUG, "NIC base address %lx\n",base);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000278
Morgan Tsai218c2652007-11-02 16:09:58 +0000279 if(!(val=phy_detect(base,&PhyAddr)))
280 {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000281 printk(BIOS_DEBUG, "PHY detect fail !!!!\r\n");
Morgan Tsai218c2652007-11-02 16:09:58 +0000282 return;
283 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000284
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000285 ulValue=read32(base + 0x38L); // check EEPROM existing
Morgan Tsai1602dd52007-10-29 21:00:14 +0000286
Morgan Tsai218c2652007-11-02 16:09:58 +0000287 if((ulValue & 0x0002))
288 {
Morgan Tsai1602dd52007-10-29 21:00:14 +0000289
Morgan Tsai218c2652007-11-02 16:09:58 +0000290 // read MAC address from EEPROM at first
291
Morgan Tsai1602dd52007-10-29 21:00:14 +0000292 // if that is valid we will use that
Morgan Tsai218c2652007-11-02 16:09:58 +0000293
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000294 printk(BIOS_DEBUG, "EEPROM contents %x \n",ReadEEprom( dev, base, 0LL));
Morgan Tsai1602dd52007-10-29 21:00:14 +0000295 for(i=0;i<3;i++) {
296 //status = smbus_read_byte(dev_eeprom, i);
297 ulValue=ReadEEprom( dev, base, i+3L);
Morgan Tsai218c2652007-11-02 16:09:58 +0000298 if (ulValue ==0x10000) break; // error
299
300 MacAddr[i] =ulValue & 0xFFFF;
301
Morgan Tsai1602dd52007-10-29 21:00:14 +0000302 }
Morgan Tsai218c2652007-11-02 16:09:58 +0000303 }else{
304 // read MAC address from firmware
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000305 printk(BIOS_DEBUG, "EEPROM invalid!!\nReg 0x38h=%.8lx \n",ulValue);
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000306 MacAddr[0]=read16(0xffffffc0); // mac address store at here
307 MacAddr[1]=read16(0xffffffc2);
308 MacAddr[2]=read16(0xffffffc4);
Morgan Tsai218c2652007-11-02 16:09:58 +0000309 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000310
Morgan Tsai218c2652007-11-02 16:09:58 +0000311 set_apc(dev);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000312
Morgan Tsai218c2652007-11-02 16:09:58 +0000313 readApcMacAddr();
Morgan Tsai1602dd52007-10-29 21:00:14 +0000314
Morgan Tsai218c2652007-11-02 16:09:58 +0000315#if DEBUG_NIC
Morgan Tsai1602dd52007-10-29 21:00:14 +0000316{
Morgan Tsai218c2652007-11-02 16:09:58 +0000317 int i;
318
319 print_debug("****** NIC PCI config ******");
320 print_debug("\n 03020100 07060504 0B0A0908 0F0E0D0C");
321
322 for(i=0;i<0xff;i+=4){
323 if((i%16)==0){
324 print_debug("\r\n");
325 print_debug_hex8(i);
326 print_debug(": ");
327 }
328 print_debug_hex32(pci_read_config32(dev,i));
329 print_debug(" ");
330 }
331 print_debug("\r\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000332}
333
Morgan Tsai218c2652007-11-02 16:09:58 +0000334
Morgan Tsai1602dd52007-10-29 21:00:14 +0000335#endif
336
337}
338
Morgan Tsai218c2652007-11-02 16:09:58 +0000339print_debug("NIC_INIT:<----------\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000340return;
341
Morgan Tsai1602dd52007-10-29 21:00:14 +0000342
343}
344
Morgan Tsai1602dd52007-10-29 21:00:14 +0000345static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
346{
347 pci_write_config32(dev, 0x40,
348 ((device & 0xffff) << 16) | (vendor & 0xffff));
349}
350
351static struct pci_operations lops_pci = {
352 .set_subsystem = lpci_set_subsystem,
353};
354
355static struct device_operations nic_ops = {
356 .read_resources = pci_dev_read_resources,
357 .set_resources = pci_dev_set_resources,
358 .enable_resources = pci_dev_enable_resources,
359 .init = nic_init,
360 .scan_bus = 0,
361// .enable = sis966_enable,
362 .ops_pci = &lops_pci,
363};
Carl-Daniel Hailfingera3588922007-11-05 22:21:27 +0000364
Stefan Reinauer83b52e72007-10-30 02:17:49 +0000365static const struct pci_driver nic_driver __pci_driver = {
Morgan Tsai1602dd52007-10-29 21:00:14 +0000366 .ops = &nic_ops,
367 .vendor = PCI_VENDOR_ID_SIS,
Morgan Tsai31e805d2007-11-14 01:34:02 +0000368 .device = PCI_DEVICE_ID_SIS_SIS966_NIC,
Morgan Tsai1602dd52007-10-29 21:00:14 +0000369};