blob: 183260fdfcb4e7ca9116fd1bedef4b0fed9d8ed2 [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
Paul Menzela46a7122013-02-23 18:37:27 +010023 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Morgan Tsai1602dd52007-10-29 21:00:14 +000024 */
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
Stefan Reinauereea66b72010-04-07 15:32:52 +000037u8 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
Stefan Reinauereea66b72010-04-07 15:32:52 +000053u16 MacAddr[3];
Morgan Tsai1602dd52007-10-29 21:00:14 +000054
55
Stefan Reinauereea66b72010-04-07 15:32:52 +000056static void writeApcByte(int addr, u8 value)
Morgan Tsai1602dd52007-10-29 21:00:14 +000057{
58 outb(addr,0x78);
Morgan Tsai218c2652007-11-02 16:09:58 +000059 outb(value,0x79);
Morgan Tsai1602dd52007-10-29 21:00:14 +000060}
Stefan Reinauereea66b72010-04-07 15:32:52 +000061
62static u8 readApcByte(int addr)
Morgan Tsai1602dd52007-10-29 21:00:14 +000063{
Stefan Reinauereea66b72010-04-07 15:32:52 +000064 u8 value;
Morgan Tsai1602dd52007-10-29 21:00:14 +000065 outb(addr,0x78);
66 value=inb(0x79);
67 return(value);
68}
69
70static void readApcMacAddr(void)
71{
Stefan Reinauereea66b72010-04-07 15:32:52 +000072 u8 i;
Morgan Tsai1602dd52007-10-29 21:00:14 +000073
74// enable APC in south bridge sis966 D2F0
75
Morgan Tsai218c2652007-11-02 16:09:58 +000076 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +000077 outl((inl(0xcfc) & 0xfffffffd),0xcfc ); // enable IO78/79h for APC Index/Data
78
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000079 printk(BIOS_DEBUG, "MAC addr in APC = ");
Morgan Tsai218c2652007-11-02 16:09:58 +000080 for(i = 0x9 ; i <=0xe ; i++)
Morgan Tsai1602dd52007-10-29 21:00:14 +000081 {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000082 printk(BIOS_DEBUG, "%2.2x",readApcByte(i));
Morgan Tsai1602dd52007-10-29 21:00:14 +000083 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000084 printk(BIOS_DEBUG, "\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +000085
86 /* Set APC Reload */
87 writeApcByte(0x7,readApcByte(0x7)&0xf7);
88 writeApcByte(0x7,readApcByte(0x7)|0x0a);
Morgan Tsai218c2652007-11-02 16:09:58 +000089
Morgan Tsai1602dd52007-10-29 21:00:14 +000090 /* disable APC in south bridge */
Morgan Tsai218c2652007-11-02 16:09:58 +000091 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +000092 outl(inl(0xcfc)&0xffffffbf,0xcfc);
93}
94
95static void set_apc(struct device *dev)
96{
Stefan Reinauereea66b72010-04-07 15:32:52 +000097 u16 addr;
98 u16 i;
99 u8 bTmp;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000100
101 /* enable APC in south bridge sis966 D2F0 */
Morgan Tsai218c2652007-11-02 16:09:58 +0000102 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000103 outl((inl(0xcfc) & 0xfffffffd),0xcfc ); // enable IO78/79h for APC Index/Data
Morgan Tsai218c2652007-11-02 16:09:58 +0000104
Morgan Tsai1602dd52007-10-29 21:00:14 +0000105 for(i = 0 ; i <3; i++)
106 {
107 addr=0x9+2*i;
Stefan Reinauereea66b72010-04-07 15:32:52 +0000108 writeApcByte(addr,(u8)(MacAddr[i]&0xFF));
109 writeApcByte(addr+1L,(u8)((MacAddr[i]>>8)&0xFF));
Morgan Tsai1602dd52007-10-29 21:00:14 +0000110 // printf("%x - ",readMacAddrByte(0x59+i));
111 }
112
113 /* Set APC Reload */
114 writeApcByte(0x7,readApcByte(0x7)&0xf7);
115 writeApcByte(0x7,readApcByte(0x7)|0x0a);
Morgan Tsai218c2652007-11-02 16:09:58 +0000116
Morgan Tsai1602dd52007-10-29 21:00:14 +0000117 /* disable APC in south bridge */
Morgan Tsai218c2652007-11-02 16:09:58 +0000118 outl(0x80001048,0xcf8);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000119 outl(inl(0xcfc)&0xffffffbf,0xcfc);
120
Morgan Tsai218c2652007-11-02 16:09:58 +0000121 // CFG reg0x73 bit=1, tell driver MAC Address load to APC
122 bTmp = pci_read_config8(dev, 0x73);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000123 bTmp|=0x1;
124 pci_write_config8(dev, 0x73, bTmp);
125}
126
Uwe Hermannb69cb5a2010-10-26 22:46:43 +0000127/**
128 * Read one word out of the serial EEPROM.
129 *
130 * @param dev TODO
131 * @param base TODO
132 * @param Reg EEPROM word to read.
133 * @return Contents of EEPROM word (Reg).
134 */
Morgan Tsai1602dd52007-10-29 21:00:14 +0000135#define LoopNum 200
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800136static unsigned long ReadEEprom( struct device *dev, u8 *base, u32 Reg)
Morgan Tsai1602dd52007-10-29 21:00:14 +0000137{
Stefan Reinauereea66b72010-04-07 15:32:52 +0000138 u32 data;
139 u32 i;
140 u32 ulValue;
Morgan Tsai218c2652007-11-02 16:09:58 +0000141
142
Morgan Tsai1602dd52007-10-29 21:00:14 +0000143 ulValue = (0x80 | (0x2 << 8) | (Reg << 10)); //BIT_7
144
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800145 write32(base + 0x3c, ulValue);
Morgan Tsai218c2652007-11-02 16:09:58 +0000146
Morgan Tsai1602dd52007-10-29 21:00:14 +0000147 mdelay(10);
148
149 for(i=0 ; i <= LoopNum; i++)
150 {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800151 ulValue=read32(base + 0x3c);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000152
153 if(!(ulValue & 0x0080)) //BIT_7
154 break;
155
156 mdelay(100);
157 }
Morgan Tsai218c2652007-11-02 16:09:58 +0000158
Morgan Tsai1602dd52007-10-29 21:00:14 +0000159 mdelay(50);
160
161 if(i==LoopNum) data=0x10000;
162 else{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800163 ulValue=read32(base + 0x3c);
Carl-Daniel Hailfingera3588922007-11-05 22:21:27 +0000164 data = ((ulValue & 0xffff0000) >> 16);
Morgan Tsai218c2652007-11-02 16:09:58 +0000165 }
166
Morgan Tsai1602dd52007-10-29 21:00:14 +0000167 return data;
168}
169
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800170static int phy_read(u8 *base, unsigned phy_addr, unsigned phy_reg)
Morgan Tsai1602dd52007-10-29 21:00:14 +0000171{
Stefan Reinauereea66b72010-04-07 15:32:52 +0000172 u32 ulValue;
173 u32 Read_Cmd;
174 u16 usData;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000175
Morgan Tsai1602dd52007-10-29 21:00:14 +0000176
Morgan Tsai218c2652007-11-02 16:09:58 +0000177
178 Read_Cmd = ((phy_reg << 11) |
Morgan Tsai1602dd52007-10-29 21:00:14 +0000179 (phy_addr << 6) |
180 SMI_READ |
181 SMI_REQUEST);
182
183 // SmiMgtInterface Reg is the SMI management interface register(offset 44h) of MAC
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800184 write32(base + 0x44, Read_Cmd);
Morgan Tsai218c2652007-11-02 16:09:58 +0000185
Morgan Tsai1602dd52007-10-29 21:00:14 +0000186 // Polling SMI_REQ bit to be deasserted indicated read command completed
187 do
188 {
189 // Wait 20 usec before checking status
Morgan Tsai1602dd52007-10-29 21:00:14 +0000190 mdelay(20);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800191 ulValue = read32(base + 0x44);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000192 } while((ulValue & SMI_REQUEST) != 0);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000193 //printk(BIOS_DEBUG, "base %x cmd %lx ret val %lx\n", tmp,Read_Cmd,ulValue);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000194 usData=(ulValue>>16);
195
Morgan Tsai218c2652007-11-02 16:09:58 +0000196
Morgan Tsai1602dd52007-10-29 21:00:14 +0000197
198 return usData;
199
200}
201
202// Detect a valid PHY
203// If there exist a valid PHY then return TRUE, else return FALSE
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800204static int phy_detect(u8 *base,u16 *PhyAddr) //BOOL PHY_Detect()
Morgan Tsai1602dd52007-10-29 21:00:14 +0000205{
206 int bFoundPhy = FALSE;
Stefan Reinauereea66b72010-04-07 15:32:52 +0000207 u16 usData;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000208 int PhyAddress = 0;
Morgan Tsai218c2652007-11-02 16:09:58 +0000209
210
Morgan Tsai1602dd52007-10-29 21:00:14 +0000211 // Scan all PHY address(0 ~ 31) to find a valid PHY
212 for(PhyAddress = 0; PhyAddress < 32; PhyAddress++)
Morgan Tsai218c2652007-11-02 16:09:58 +0000213 {
214 usData=phy_read(base,PhyAddress,StatusReg); // Status register is a PHY's register(offset 01h)
215
216 // Found a valid PHY
217
Morgan Tsai1602dd52007-10-29 21:00:14 +0000218 if((usData != 0x0) && (usData != 0xffff))
219 {
220 bFoundPhy = TRUE;
221 break;
222 }
223 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000224
Morgan Tsai1602dd52007-10-29 21:00:14 +0000225
226 if(!bFoundPhy)
227 {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000228 printk(BIOS_DEBUG, "PHY not found !!!! \n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000229 }
230
231 *PhyAddr=PhyAddress;
Morgan Tsai218c2652007-11-02 16:09:58 +0000232
Morgan Tsai1602dd52007-10-29 21:00:14 +0000233 return bFoundPhy;
234}
235
236
237static void nic_init(struct device *dev)
238{
Morgan Tsai218c2652007-11-02 16:09:58 +0000239 int val;
Stefan Reinauereea66b72010-04-07 15:32:52 +0000240 u16 PhyAddr;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800241 u8 *base;
Morgan Tsai218c2652007-11-02 16:09:58 +0000242 struct resource *res;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000243
Stefan Reinauer5ab52dd2015-01-05 13:01:01 -0800244 printk(BIOS_DEBUG, "NIC_INIT:---------->\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000245
Morgan Tsai1602dd52007-10-29 21:00:14 +0000246//-------------- enable NIC (SiS19x) -------------------------
247{
Stefan Reinauereea66b72010-04-07 15:32:52 +0000248 u8 temp8;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000249 int i=0;
250 while(SiS_SiS191_init[i][0] != 0)
Morgan Tsai218c2652007-11-02 16:09:58 +0000251 {
252 temp8 = pci_read_config8(dev, SiS_SiS191_init[i][0]);
253 temp8 &= SiS_SiS191_init[i][1];
254 temp8 |= SiS_SiS191_init[i][2];
255 pci_write_config8(dev, SiS_SiS191_init[i][0], temp8);
256 i++;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000257 };
258}
259//-----------------------------------------------------------
260
Morgan Tsai1602dd52007-10-29 21:00:14 +0000261{
Morgan Tsai218c2652007-11-02 16:09:58 +0000262 unsigned long i;
263 unsigned long ulValue;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000264
Morgan Tsai1602dd52007-10-29 21:00:14 +0000265 res = find_resource(dev, 0x10);
266
Morgan Tsai218c2652007-11-02 16:09:58 +0000267 if(!res)
268 {
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000269 printk(BIOS_DEBUG, "NIC Cannot find resource..\n");
Morgan Tsai218c2652007-11-02 16:09:58 +0000270 return;
271 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800272 base = res2mmio(res, 0, 0);
273 printk(BIOS_DEBUG, "NIC base address %p\n",base);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000274
Morgan Tsai218c2652007-11-02 16:09:58 +0000275 if(!(val=phy_detect(base,&PhyAddr)))
276 {
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000277 printk(BIOS_DEBUG, "PHY detect fail !!!!\n");
Morgan Tsai218c2652007-11-02 16:09:58 +0000278 return;
279 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000280
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000281 ulValue=read32(base + 0x38L); // check EEPROM existing
Morgan Tsai1602dd52007-10-29 21:00:14 +0000282
Morgan Tsai218c2652007-11-02 16:09:58 +0000283 if((ulValue & 0x0002))
284 {
Morgan Tsai1602dd52007-10-29 21:00:14 +0000285
Morgan Tsai218c2652007-11-02 16:09:58 +0000286 // read MAC address from EEPROM at first
287
Morgan Tsai1602dd52007-10-29 21:00:14 +0000288 // if that is valid we will use that
Morgan Tsai218c2652007-11-02 16:09:58 +0000289
Myles Watson08e0fb82010-03-22 16:33:25 +0000290 printk(BIOS_DEBUG, "EEPROM contents %lx \n",ReadEEprom( dev, base, 0LL));
Morgan Tsai1602dd52007-10-29 21:00:14 +0000291 for(i=0;i<3;i++) {
292 //status = smbus_read_byte(dev_eeprom, i);
293 ulValue=ReadEEprom( dev, base, i+3L);
Morgan Tsai218c2652007-11-02 16:09:58 +0000294 if (ulValue ==0x10000) break; // error
295
296 MacAddr[i] =ulValue & 0xFFFF;
297
Morgan Tsai1602dd52007-10-29 21:00:14 +0000298 }
Morgan Tsai218c2652007-11-02 16:09:58 +0000299 }else{
300 // read MAC address from firmware
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000301 printk(BIOS_DEBUG, "EEPROM invalid!!\nReg 0x38h=%.8lx \n",ulValue);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800302 MacAddr[0]=read16((u16 *)0xffffffc0); // mac address store at here
303 MacAddr[1]=read16((u16 *)0xffffffc2);
304 MacAddr[2]=read16((u16 *)0xffffffc4);
Morgan Tsai218c2652007-11-02 16:09:58 +0000305 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000306
Morgan Tsai218c2652007-11-02 16:09:58 +0000307 set_apc(dev);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000308
Morgan Tsai218c2652007-11-02 16:09:58 +0000309 readApcMacAddr();
Morgan Tsai1602dd52007-10-29 21:00:14 +0000310
Morgan Tsai218c2652007-11-02 16:09:58 +0000311#if DEBUG_NIC
Morgan Tsai1602dd52007-10-29 21:00:14 +0000312{
Morgan Tsai218c2652007-11-02 16:09:58 +0000313 int i;
314
Stefan Reinauer5ab52dd2015-01-05 13:01:01 -0800315 printk(BIOS_DEBUG, "****** NIC PCI config ******");
316 printk(BIOS_DEBUG, "\n 03020100 07060504 0B0A0908 0F0E0D0C");
Morgan Tsai218c2652007-11-02 16:09:58 +0000317
318 for(i=0;i<0xff;i+=4){
Stefan Reinauer5ab52dd2015-01-05 13:01:01 -0800319 if((i%16)==0)
320 printk(BIOS_DEBUG, "\n%02x: ", i);
321 printk(BIOS_DEBUG, "%08x ", pci_read_config32(dev,i));
Morgan Tsai218c2652007-11-02 16:09:58 +0000322 }
Stefan Reinauer5ab52dd2015-01-05 13:01:01 -0800323 printk(BIOS_DEBUG, "\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000324}
325
Morgan Tsai218c2652007-11-02 16:09:58 +0000326
Morgan Tsai1602dd52007-10-29 21:00:14 +0000327#endif
328
329}
330
Stefan Reinauer5ab52dd2015-01-05 13:01:01 -0800331printk(BIOS_DEBUG, "NIC_INIT:<----------\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000332return;
333
Morgan Tsai1602dd52007-10-29 21:00:14 +0000334
335}
336
Morgan Tsai1602dd52007-10-29 21:00:14 +0000337static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
338{
339 pci_write_config32(dev, 0x40,
340 ((device & 0xffff) << 16) | (vendor & 0xffff));
341}
342
343static struct pci_operations lops_pci = {
344 .set_subsystem = lpci_set_subsystem,
345};
346
347static struct device_operations nic_ops = {
348 .read_resources = pci_dev_read_resources,
349 .set_resources = pci_dev_set_resources,
350 .enable_resources = pci_dev_enable_resources,
351 .init = nic_init,
352 .scan_bus = 0,
353// .enable = sis966_enable,
354 .ops_pci = &lops_pci,
355};
Carl-Daniel Hailfingera3588922007-11-05 22:21:27 +0000356
Stefan Reinauer83b52e72007-10-30 02:17:49 +0000357static const struct pci_driver nic_driver __pci_driver = {
Morgan Tsai1602dd52007-10-29 21:00:14 +0000358 .ops = &nic_ops,
359 .vendor = PCI_VENDOR_ID_SIS,
Morgan Tsai31e805d2007-11-14 01:34:02 +0000360 .device = PCI_DEVICE_ID_SIS_SIS966_NIC,
Morgan Tsai1602dd52007-10-29 21:00:14 +0000361};