/kern_oII/drivers/net/pcmcia/smc91c92_cs.c

http://omnia2droid.googlecode.com/ · C · 2267 lines · 1672 code · 326 blank · 269 comment · 294 complexity · b069d80663c985afb1c5d751677a11e4 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*======================================================================
  2. A PCMCIA ethernet driver for SMC91c92-based cards.
  3. This driver supports Megahertz PCMCIA ethernet cards; and
  4. Megahertz, Motorola, Ositech, and Psion Dacom ethernet/modem
  5. multifunction cards.
  6. Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
  7. smc91c92_cs.c 1.122 2002/10/25 06:26:39
  8. This driver contains code written by Donald Becker
  9. (becker@scyld.com), Rowan Hughes (x-csrdh@jcu.edu.au),
  10. David Hinds (dahinds@users.sourceforge.net), and Erik Stahlman
  11. (erik@vt.edu). Donald wrote the SMC 91c92 code using parts of
  12. Erik's SMC 91c94 driver. Rowan wrote a similar driver, and I've
  13. incorporated some parts of his driver here. I (Dave) wrote most
  14. of the PCMCIA glue code, and the Ositech support code. Kelly
  15. Stephens (kstephen@holli.com) added support for the Motorola
  16. Mariner, with help from Allen Brost.
  17. This software may be used and distributed according to the terms of
  18. the GNU General Public License, incorporated herein by reference.
  19. ======================================================================*/
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/timer.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/crc32.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/ioport.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/mii.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/firmware.h>
  38. #include <pcmcia/cs_types.h>
  39. #include <pcmcia/cs.h>
  40. #include <pcmcia/cistpl.h>
  41. #include <pcmcia/cisreg.h>
  42. #include <pcmcia/ciscode.h>
  43. #include <pcmcia/ds.h>
  44. #include <pcmcia/ss.h>
  45. #include <asm/io.h>
  46. #include <asm/system.h>
  47. #include <asm/uaccess.h>
  48. /*====================================================================*/
  49. static const char *if_names[] = { "auto", "10baseT", "10base2"};
  50. /* Firmware name */
  51. #define FIRMWARE_NAME "ositech/Xilinx7OD.bin"
  52. /* Module parameters */
  53. MODULE_DESCRIPTION("SMC 91c92 series PCMCIA ethernet driver");
  54. MODULE_LICENSE("GPL");
  55. MODULE_FIRMWARE(FIRMWARE_NAME);
  56. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  57. /*
  58. Transceiver/media type.
  59. 0 = auto
  60. 1 = 10baseT (and autoselect if #define AUTOSELECT),
  61. 2 = AUI/10base2,
  62. */
  63. INT_MODULE_PARM(if_port, 0);
  64. #ifdef PCMCIA_DEBUG
  65. INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
  66. static const char *version =
  67. "smc91c92_cs.c 1.123 2006/11/09 Donald Becker, becker@scyld.com.\n";
  68. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  69. #else
  70. #define DEBUG(n, args...)
  71. #endif
  72. #define DRV_NAME "smc91c92_cs"
  73. #define DRV_VERSION "1.123"
  74. /*====================================================================*/
  75. /* Operational parameter that usually are not changed. */
  76. /* Time in jiffies before concluding Tx hung */
  77. #define TX_TIMEOUT ((400*HZ)/1000)
  78. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  79. #define INTR_WORK 4
  80. /* Times to check the check the chip before concluding that it doesn't
  81. currently have room for another Tx packet. */
  82. #define MEMORY_WAIT_TIME 8
  83. struct smc_private {
  84. struct pcmcia_device *p_dev;
  85. spinlock_t lock;
  86. u_short manfid;
  87. u_short cardid;
  88. dev_node_t node;
  89. struct sk_buff *saved_skb;
  90. int packets_waiting;
  91. void __iomem *base;
  92. u_short cfg;
  93. struct timer_list media;
  94. int watchdog, tx_err;
  95. u_short media_status;
  96. u_short fast_poll;
  97. u_short link_status;
  98. struct mii_if_info mii_if;
  99. int duplex;
  100. int rx_ovrn;
  101. };
  102. struct smc_cfg_mem {
  103. tuple_t tuple;
  104. cisparse_t parse;
  105. u_char buf[255];
  106. };
  107. /* Special definitions for Megahertz multifunction cards */
  108. #define MEGAHERTZ_ISR 0x0380
  109. /* Special function registers for Motorola Mariner */
  110. #define MOT_LAN 0x0000
  111. #define MOT_UART 0x0020
  112. #define MOT_EEPROM 0x20
  113. #define MOT_NORMAL \
  114. (COR_LEVEL_REQ | COR_FUNC_ENA | COR_ADDR_DECODE | COR_IREQ_ENA)
  115. /* Special function registers for Ositech cards */
  116. #define OSITECH_AUI_CTL 0x0c
  117. #define OSITECH_PWRDOWN 0x0d
  118. #define OSITECH_RESET 0x0e
  119. #define OSITECH_ISR 0x0f
  120. #define OSITECH_AUI_PWR 0x0c
  121. #define OSITECH_RESET_ISR 0x0e
  122. #define OSI_AUI_PWR 0x40
  123. #define OSI_LAN_PWRDOWN 0x02
  124. #define OSI_MODEM_PWRDOWN 0x01
  125. #define OSI_LAN_RESET 0x02
  126. #define OSI_MODEM_RESET 0x01
  127. /* Symbolic constants for the SMC91c9* series chips, from Erik Stahlman. */
  128. #define BANK_SELECT 14 /* Window select register. */
  129. #define SMC_SELECT_BANK(x) { outw(x, ioaddr + BANK_SELECT); }
  130. /* Bank 0 registers. */
  131. #define TCR 0 /* transmit control register */
  132. #define TCR_CLEAR 0 /* do NOTHING */
  133. #define TCR_ENABLE 0x0001 /* if this is 1, we can transmit */
  134. #define TCR_PAD_EN 0x0080 /* pads short packets to 64 bytes */
  135. #define TCR_MONCSN 0x0400 /* Monitor Carrier. */
  136. #define TCR_FDUPLX 0x0800 /* Full duplex mode. */
  137. #define TCR_NORMAL TCR_ENABLE | TCR_PAD_EN
  138. #define EPH 2 /* Ethernet Protocol Handler report. */
  139. #define EPH_TX_SUC 0x0001
  140. #define EPH_SNGLCOL 0x0002
  141. #define EPH_MULCOL 0x0004
  142. #define EPH_LTX_MULT 0x0008
  143. #define EPH_16COL 0x0010
  144. #define EPH_SQET 0x0020
  145. #define EPH_LTX_BRD 0x0040
  146. #define EPH_TX_DEFR 0x0080
  147. #define EPH_LAT_COL 0x0200
  148. #define EPH_LOST_CAR 0x0400
  149. #define EPH_EXC_DEF 0x0800
  150. #define EPH_CTR_ROL 0x1000
  151. #define EPH_RX_OVRN 0x2000
  152. #define EPH_LINK_OK 0x4000
  153. #define EPH_TX_UNRN 0x8000
  154. #define MEMINFO 8 /* Memory Information Register */
  155. #define MEMCFG 10 /* Memory Configuration Register */
  156. /* Bank 1 registers. */
  157. #define CONFIG 0
  158. #define CFG_MII_SELECT 0x8000 /* 91C100 only */
  159. #define CFG_NO_WAIT 0x1000
  160. #define CFG_FULL_STEP 0x0400
  161. #define CFG_SET_SQLCH 0x0200
  162. #define CFG_AUI_SELECT 0x0100
  163. #define CFG_16BIT 0x0080
  164. #define CFG_DIS_LINK 0x0040
  165. #define CFG_STATIC 0x0030
  166. #define CFG_IRQ_SEL_1 0x0004
  167. #define CFG_IRQ_SEL_0 0x0002
  168. #define BASE_ADDR 2
  169. #define ADDR0 4
  170. #define GENERAL 10
  171. #define CONTROL 12
  172. #define CTL_STORE 0x0001
  173. #define CTL_RELOAD 0x0002
  174. #define CTL_EE_SELECT 0x0004
  175. #define CTL_TE_ENABLE 0x0020
  176. #define CTL_CR_ENABLE 0x0040
  177. #define CTL_LE_ENABLE 0x0080
  178. #define CTL_AUTO_RELEASE 0x0800
  179. #define CTL_POWERDOWN 0x2000
  180. /* Bank 2 registers. */
  181. #define MMU_CMD 0
  182. #define MC_ALLOC 0x20 /* or with number of 256 byte packets */
  183. #define MC_RESET 0x40
  184. #define MC_RELEASE 0x80 /* remove and release the current rx packet */
  185. #define MC_FREEPKT 0xA0 /* Release packet in PNR register */
  186. #define MC_ENQUEUE 0xC0 /* Enqueue the packet for transmit */
  187. #define PNR_ARR 2
  188. #define FIFO_PORTS 4
  189. #define FP_RXEMPTY 0x8000
  190. #define POINTER 6
  191. #define PTR_AUTO_INC 0x0040
  192. #define PTR_READ 0x2000
  193. #define PTR_AUTOINC 0x4000
  194. #define PTR_RCV 0x8000
  195. #define DATA_1 8
  196. #define INTERRUPT 12
  197. #define IM_RCV_INT 0x1
  198. #define IM_TX_INT 0x2
  199. #define IM_TX_EMPTY_INT 0x4
  200. #define IM_ALLOC_INT 0x8
  201. #define IM_RX_OVRN_INT 0x10
  202. #define IM_EPH_INT 0x20
  203. #define RCR 4
  204. enum RxCfg { RxAllMulti = 0x0004, RxPromisc = 0x0002,
  205. RxEnable = 0x0100, RxStripCRC = 0x0200};
  206. #define RCR_SOFTRESET 0x8000 /* resets the chip */
  207. #define RCR_STRIP_CRC 0x200 /* strips CRC */
  208. #define RCR_ENABLE 0x100 /* IFF this is set, we can receive packets */
  209. #define RCR_ALMUL 0x4 /* receive all multicast packets */
  210. #define RCR_PROMISC 0x2 /* enable promiscuous mode */
  211. /* the normal settings for the RCR register : */
  212. #define RCR_NORMAL (RCR_STRIP_CRC | RCR_ENABLE)
  213. #define RCR_CLEAR 0x0 /* set it to a base state */
  214. #define COUNTER 6
  215. /* BANK 3 -- not the same values as in smc9194! */
  216. #define MULTICAST0 0
  217. #define MULTICAST2 2
  218. #define MULTICAST4 4
  219. #define MULTICAST6 6
  220. #define MGMT 8
  221. #define REVISION 0x0a
  222. /* Transmit status bits. */
  223. #define TS_SUCCESS 0x0001
  224. #define TS_16COL 0x0010
  225. #define TS_LATCOL 0x0200
  226. #define TS_LOSTCAR 0x0400
  227. /* Receive status bits. */
  228. #define RS_ALGNERR 0x8000
  229. #define RS_BADCRC 0x2000
  230. #define RS_ODDFRAME 0x1000
  231. #define RS_TOOLONG 0x0800
  232. #define RS_TOOSHORT 0x0400
  233. #define RS_MULTICAST 0x0001
  234. #define RS_ERRORS (RS_ALGNERR | RS_BADCRC | RS_TOOLONG | RS_TOOSHORT)
  235. #define set_bits(v, p) outw(inw(p)|(v), (p))
  236. #define mask_bits(v, p) outw(inw(p)&(v), (p))
  237. /*====================================================================*/
  238. static void smc91c92_detach(struct pcmcia_device *p_dev);
  239. static int smc91c92_config(struct pcmcia_device *link);
  240. static void smc91c92_release(struct pcmcia_device *link);
  241. static int smc_open(struct net_device *dev);
  242. static int smc_close(struct net_device *dev);
  243. static int smc_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  244. static void smc_tx_timeout(struct net_device *dev);
  245. static int smc_start_xmit(struct sk_buff *skb, struct net_device *dev);
  246. static irqreturn_t smc_interrupt(int irq, void *dev_id);
  247. static void smc_rx(struct net_device *dev);
  248. static void set_rx_mode(struct net_device *dev);
  249. static int s9k_config(struct net_device *dev, struct ifmap *map);
  250. static void smc_set_xcvr(struct net_device *dev, int if_port);
  251. static void smc_reset(struct net_device *dev);
  252. static void media_check(u_long arg);
  253. static void mdio_sync(unsigned int addr);
  254. static int mdio_read(struct net_device *dev, int phy_id, int loc);
  255. static void mdio_write(struct net_device *dev, int phy_id, int loc, int value);
  256. static int smc_link_ok(struct net_device *dev);
  257. static const struct ethtool_ops ethtool_ops;
  258. static const struct net_device_ops smc_netdev_ops = {
  259. .ndo_open = smc_open,
  260. .ndo_stop = smc_close,
  261. .ndo_start_xmit = smc_start_xmit,
  262. .ndo_tx_timeout = smc_tx_timeout,
  263. .ndo_set_config = s9k_config,
  264. .ndo_set_multicast_list = set_rx_mode,
  265. .ndo_do_ioctl = &smc_ioctl,
  266. .ndo_change_mtu = eth_change_mtu,
  267. .ndo_set_mac_address = eth_mac_addr,
  268. .ndo_validate_addr = eth_validate_addr,
  269. };
  270. /*======================================================================
  271. smc91c92_attach() creates an "instance" of the driver, allocating
  272. local data structures for one device. The device is registered
  273. with Card Services.
  274. ======================================================================*/
  275. static int smc91c92_probe(struct pcmcia_device *link)
  276. {
  277. struct smc_private *smc;
  278. struct net_device *dev;
  279. DEBUG(0, "smc91c92_attach()\n");
  280. /* Create new ethernet device */
  281. dev = alloc_etherdev(sizeof(struct smc_private));
  282. if (!dev)
  283. return -ENOMEM;
  284. smc = netdev_priv(dev);
  285. smc->p_dev = link;
  286. link->priv = dev;
  287. spin_lock_init(&smc->lock);
  288. link->io.NumPorts1 = 16;
  289. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  290. link->io.IOAddrLines = 4;
  291. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_HANDLE_PRESENT;
  292. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  293. link->irq.Handler = &smc_interrupt;
  294. link->irq.Instance = dev;
  295. link->conf.Attributes = CONF_ENABLE_IRQ;
  296. link->conf.IntType = INT_MEMORY_AND_IO;
  297. /* The SMC91c92-specific entries in the device structure. */
  298. dev->netdev_ops = &smc_netdev_ops;
  299. SET_ETHTOOL_OPS(dev, &ethtool_ops);
  300. dev->watchdog_timeo = TX_TIMEOUT;
  301. smc->mii_if.dev = dev;
  302. smc->mii_if.mdio_read = mdio_read;
  303. smc->mii_if.mdio_write = mdio_write;
  304. smc->mii_if.phy_id_mask = 0x1f;
  305. smc->mii_if.reg_num_mask = 0x1f;
  306. return smc91c92_config(link);
  307. } /* smc91c92_attach */
  308. /*======================================================================
  309. This deletes a driver "instance". The device is de-registered
  310. with Card Services. If it has been released, all local data
  311. structures are freed. Otherwise, the structures will be freed
  312. when the device is released.
  313. ======================================================================*/
  314. static void smc91c92_detach(struct pcmcia_device *link)
  315. {
  316. struct net_device *dev = link->priv;
  317. DEBUG(0, "smc91c92_detach(0x%p)\n", link);
  318. if (link->dev_node)
  319. unregister_netdev(dev);
  320. smc91c92_release(link);
  321. free_netdev(dev);
  322. } /* smc91c92_detach */
  323. /*====================================================================*/
  324. static int cvt_ascii_address(struct net_device *dev, char *s)
  325. {
  326. int i, j, da, c;
  327. if (strlen(s) != 12)
  328. return -1;
  329. for (i = 0; i < 6; i++) {
  330. da = 0;
  331. for (j = 0; j < 2; j++) {
  332. c = *s++;
  333. da <<= 4;
  334. da += ((c >= '0') && (c <= '9')) ?
  335. (c - '0') : ((c & 0x0f) + 9);
  336. }
  337. dev->dev_addr[i] = da;
  338. }
  339. return 0;
  340. }
  341. /*====================================================================*/
  342. static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  343. cisparse_t *parse)
  344. {
  345. int i;
  346. i = pcmcia_get_first_tuple(handle, tuple);
  347. if (i != 0)
  348. return i;
  349. i = pcmcia_get_tuple_data(handle, tuple);
  350. if (i != 0)
  351. return i;
  352. return pcmcia_parse_tuple(tuple, parse);
  353. }
  354. static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  355. cisparse_t *parse)
  356. {
  357. int i;
  358. if ((i = pcmcia_get_next_tuple(handle, tuple)) != 0 ||
  359. (i = pcmcia_get_tuple_data(handle, tuple)) != 0)
  360. return i;
  361. return pcmcia_parse_tuple(tuple, parse);
  362. }
  363. /*======================================================================
  364. Configuration stuff for Megahertz cards
  365. mhz_3288_power() is used to power up a 3288's ethernet chip.
  366. mhz_mfc_config() handles socket setup for multifunction (1144
  367. and 3288) cards. mhz_setup() gets a card's hardware ethernet
  368. address.
  369. ======================================================================*/
  370. static int mhz_3288_power(struct pcmcia_device *link)
  371. {
  372. struct net_device *dev = link->priv;
  373. struct smc_private *smc = netdev_priv(dev);
  374. u_char tmp;
  375. /* Read the ISR twice... */
  376. readb(smc->base+MEGAHERTZ_ISR);
  377. udelay(5);
  378. readb(smc->base+MEGAHERTZ_ISR);
  379. /* Pause 200ms... */
  380. mdelay(200);
  381. /* Now read and write the COR... */
  382. tmp = readb(smc->base + link->conf.ConfigBase + CISREG_COR);
  383. udelay(5);
  384. writeb(tmp, smc->base + link->conf.ConfigBase + CISREG_COR);
  385. return 0;
  386. }
  387. static int mhz_mfc_config_check(struct pcmcia_device *p_dev,
  388. cistpl_cftable_entry_t *cf,
  389. cistpl_cftable_entry_t *dflt,
  390. unsigned int vcc,
  391. void *priv_data)
  392. {
  393. int k;
  394. p_dev->io.BasePort2 = cf->io.win[0].base;
  395. for (k = 0; k < 0x400; k += 0x10) {
  396. if (k & 0x80)
  397. continue;
  398. p_dev->io.BasePort1 = k ^ 0x300;
  399. if (!pcmcia_request_io(p_dev, &p_dev->io))
  400. return 0;
  401. }
  402. return -ENODEV;
  403. }
  404. static int mhz_mfc_config(struct pcmcia_device *link)
  405. {
  406. struct net_device *dev = link->priv;
  407. struct smc_private *smc = netdev_priv(dev);
  408. struct smc_cfg_mem *cfg_mem;
  409. win_req_t req;
  410. memreq_t mem;
  411. int i;
  412. cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
  413. if (!cfg_mem)
  414. return -ENOMEM;
  415. link->conf.Attributes |= CONF_ENABLE_SPKR;
  416. link->conf.Status = CCSR_AUDIO_ENA;
  417. link->irq.Attributes =
  418. IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED|IRQ_HANDLE_PRESENT;
  419. link->io.IOAddrLines = 16;
  420. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  421. link->io.NumPorts2 = 8;
  422. /* The Megahertz combo cards have modem-like CIS entries, so
  423. we have to explicitly try a bunch of port combinations. */
  424. if (pcmcia_loop_config(link, mhz_mfc_config_check, NULL))
  425. goto free_cfg_mem;
  426. dev->base_addr = link->io.BasePort1;
  427. /* Allocate a memory window, for accessing the ISR */
  428. req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
  429. req.Base = req.Size = 0;
  430. req.AccessSpeed = 0;
  431. i = pcmcia_request_window(&link, &req, &link->win);
  432. if (i != 0)
  433. goto free_cfg_mem;
  434. smc->base = ioremap(req.Base, req.Size);
  435. mem.CardOffset = mem.Page = 0;
  436. if (smc->manfid == MANFID_MOTOROLA)
  437. mem.CardOffset = link->conf.ConfigBase;
  438. i = pcmcia_map_mem_page(link->win, &mem);
  439. if ((i == 0)
  440. && (smc->manfid == MANFID_MEGAHERTZ)
  441. && (smc->cardid == PRODID_MEGAHERTZ_EM3288))
  442. mhz_3288_power(link);
  443. free_cfg_mem:
  444. kfree(cfg_mem);
  445. return -ENODEV;
  446. }
  447. static int mhz_setup(struct pcmcia_device *link)
  448. {
  449. struct net_device *dev = link->priv;
  450. struct smc_cfg_mem *cfg_mem;
  451. tuple_t *tuple;
  452. cisparse_t *parse;
  453. u_char *buf, *station_addr;
  454. int rc;
  455. cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
  456. if (!cfg_mem)
  457. return -1;
  458. tuple = &cfg_mem->tuple;
  459. parse = &cfg_mem->parse;
  460. buf = cfg_mem->buf;
  461. tuple->Attributes = tuple->TupleOffset = 0;
  462. tuple->TupleData = (cisdata_t *)buf;
  463. tuple->TupleDataMax = 255;
  464. /* Read the station address from the CIS. It is stored as the last
  465. (fourth) string in the Version 1 Version/ID tuple. */
  466. tuple->DesiredTuple = CISTPL_VERS_1;
  467. if (first_tuple(link, tuple, parse) != 0) {
  468. rc = -1;
  469. goto free_cfg_mem;
  470. }
  471. /* Ugh -- the EM1144 card has two VERS_1 tuples!?! */
  472. if (next_tuple(link, tuple, parse) != 0)
  473. first_tuple(link, tuple, parse);
  474. if (parse->version_1.ns > 3) {
  475. station_addr = parse->version_1.str + parse->version_1.ofs[3];
  476. if (cvt_ascii_address(dev, station_addr) == 0) {
  477. rc = 0;
  478. goto free_cfg_mem;
  479. }
  480. }
  481. /* Another possibility: for the EM3288, in a special tuple */
  482. tuple->DesiredTuple = 0x81;
  483. if (pcmcia_get_first_tuple(link, tuple) != 0) {
  484. rc = -1;
  485. goto free_cfg_mem;
  486. }
  487. if (pcmcia_get_tuple_data(link, tuple) != 0) {
  488. rc = -1;
  489. goto free_cfg_mem;
  490. }
  491. buf[12] = '\0';
  492. if (cvt_ascii_address(dev, buf) == 0) {
  493. rc = 0;
  494. goto free_cfg_mem;
  495. }
  496. rc = -1;
  497. free_cfg_mem:
  498. kfree(cfg_mem);
  499. return rc;
  500. }
  501. /*======================================================================
  502. Configuration stuff for the Motorola Mariner
  503. mot_config() writes directly to the Mariner configuration
  504. registers because the CIS is just bogus.
  505. ======================================================================*/
  506. static void mot_config(struct pcmcia_device *link)
  507. {
  508. struct net_device *dev = link->priv;
  509. struct smc_private *smc = netdev_priv(dev);
  510. unsigned int ioaddr = dev->base_addr;
  511. unsigned int iouart = link->io.BasePort2;
  512. /* Set UART base address and force map with COR bit 1 */
  513. writeb(iouart & 0xff, smc->base + MOT_UART + CISREG_IOBASE_0);
  514. writeb((iouart >> 8) & 0xff, smc->base + MOT_UART + CISREG_IOBASE_1);
  515. writeb(MOT_NORMAL, smc->base + MOT_UART + CISREG_COR);
  516. /* Set SMC base address and force map with COR bit 1 */
  517. writeb(ioaddr & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_0);
  518. writeb((ioaddr >> 8) & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_1);
  519. writeb(MOT_NORMAL, smc->base + MOT_LAN + CISREG_COR);
  520. /* Wait for things to settle down */
  521. mdelay(100);
  522. }
  523. static int mot_setup(struct pcmcia_device *link)
  524. {
  525. struct net_device *dev = link->priv;
  526. unsigned int ioaddr = dev->base_addr;
  527. int i, wait, loop;
  528. u_int addr;
  529. /* Read Ethernet address from Serial EEPROM */
  530. for (i = 0; i < 3; i++) {
  531. SMC_SELECT_BANK(2);
  532. outw(MOT_EEPROM + i, ioaddr + POINTER);
  533. SMC_SELECT_BANK(1);
  534. outw((CTL_RELOAD | CTL_EE_SELECT), ioaddr + CONTROL);
  535. for (loop = wait = 0; loop < 200; loop++) {
  536. udelay(10);
  537. wait = ((CTL_RELOAD | CTL_STORE) & inw(ioaddr + CONTROL));
  538. if (wait == 0) break;
  539. }
  540. if (wait)
  541. return -1;
  542. addr = inw(ioaddr + GENERAL);
  543. dev->dev_addr[2*i] = addr & 0xff;
  544. dev->dev_addr[2*i+1] = (addr >> 8) & 0xff;
  545. }
  546. return 0;
  547. }
  548. /*====================================================================*/
  549. static int smc_configcheck(struct pcmcia_device *p_dev,
  550. cistpl_cftable_entry_t *cf,
  551. cistpl_cftable_entry_t *dflt,
  552. unsigned int vcc,
  553. void *priv_data)
  554. {
  555. p_dev->io.BasePort1 = cf->io.win[0].base;
  556. p_dev->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
  557. return pcmcia_request_io(p_dev, &p_dev->io);
  558. }
  559. static int smc_config(struct pcmcia_device *link)
  560. {
  561. struct net_device *dev = link->priv;
  562. int i;
  563. link->io.NumPorts1 = 16;
  564. i = pcmcia_loop_config(link, smc_configcheck, NULL);
  565. if (!i)
  566. dev->base_addr = link->io.BasePort1;
  567. return i;
  568. }
  569. static int smc_setup(struct pcmcia_device *link)
  570. {
  571. struct net_device *dev = link->priv;
  572. struct smc_cfg_mem *cfg_mem;
  573. tuple_t *tuple;
  574. cisparse_t *parse;
  575. cistpl_lan_node_id_t *node_id;
  576. u_char *buf, *station_addr;
  577. int i, rc;
  578. cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
  579. if (!cfg_mem)
  580. return -ENOMEM;
  581. tuple = &cfg_mem->tuple;
  582. parse = &cfg_mem->parse;
  583. buf = cfg_mem->buf;
  584. tuple->Attributes = tuple->TupleOffset = 0;
  585. tuple->TupleData = (cisdata_t *)buf;
  586. tuple->TupleDataMax = 255;
  587. /* Check for a LAN function extension tuple */
  588. tuple->DesiredTuple = CISTPL_FUNCE;
  589. i = first_tuple(link, tuple, parse);
  590. while (i == 0) {
  591. if (parse->funce.type == CISTPL_FUNCE_LAN_NODE_ID)
  592. break;
  593. i = next_tuple(link, tuple, parse);
  594. }
  595. if (i == 0) {
  596. node_id = (cistpl_lan_node_id_t *)parse->funce.data;
  597. if (node_id->nb == 6) {
  598. for (i = 0; i < 6; i++)
  599. dev->dev_addr[i] = node_id->id[i];
  600. rc = 0;
  601. goto free_cfg_mem;
  602. }
  603. }
  604. /* Try the third string in the Version 1 Version/ID tuple. */
  605. if (link->prod_id[2]) {
  606. station_addr = link->prod_id[2];
  607. if (cvt_ascii_address(dev, station_addr) == 0) {
  608. rc = 0;
  609. goto free_cfg_mem;
  610. }
  611. }
  612. rc = -1;
  613. free_cfg_mem:
  614. kfree(cfg_mem);
  615. return rc;
  616. }
  617. /*====================================================================*/
  618. static int osi_config(struct pcmcia_device *link)
  619. {
  620. struct net_device *dev = link->priv;
  621. static const unsigned int com[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
  622. int i, j;
  623. link->conf.Attributes |= CONF_ENABLE_SPKR;
  624. link->conf.Status = CCSR_AUDIO_ENA;
  625. link->irq.Attributes =
  626. IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED|IRQ_HANDLE_PRESENT;
  627. link->io.NumPorts1 = 64;
  628. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  629. link->io.NumPorts2 = 8;
  630. link->io.IOAddrLines = 16;
  631. /* Enable Hard Decode, LAN, Modem */
  632. link->conf.ConfigIndex = 0x23;
  633. for (i = j = 0; j < 4; j++) {
  634. link->io.BasePort2 = com[j];
  635. i = pcmcia_request_io(link, &link->io);
  636. if (i == 0)
  637. break;
  638. }
  639. if (i != 0) {
  640. /* Fallback: turn off hard decode */
  641. link->conf.ConfigIndex = 0x03;
  642. link->io.NumPorts2 = 0;
  643. i = pcmcia_request_io(link, &link->io);
  644. }
  645. dev->base_addr = link->io.BasePort1 + 0x10;
  646. return i;
  647. }
  648. static int osi_load_firmware(struct pcmcia_device *link)
  649. {
  650. const struct firmware *fw;
  651. int i, err;
  652. err = request_firmware(&fw, FIRMWARE_NAME, &link->dev);
  653. if (err) {
  654. pr_err("Failed to load firmware \"%s\"\n", FIRMWARE_NAME);
  655. return err;
  656. }
  657. /* Download the Seven of Diamonds firmware */
  658. for (i = 0; i < fw->size; i++) {
  659. outb(fw->data[i], link->io.BasePort1 + 2);
  660. udelay(50);
  661. }
  662. release_firmware(fw);
  663. return err;
  664. }
  665. static int osi_setup(struct pcmcia_device *link, u_short manfid, u_short cardid)
  666. {
  667. struct net_device *dev = link->priv;
  668. struct smc_cfg_mem *cfg_mem;
  669. tuple_t *tuple;
  670. u_char *buf;
  671. int i, rc;
  672. cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
  673. if (!cfg_mem)
  674. return -1;
  675. tuple = &cfg_mem->tuple;
  676. buf = cfg_mem->buf;
  677. tuple->Attributes = TUPLE_RETURN_COMMON;
  678. tuple->TupleData = (cisdata_t *)buf;
  679. tuple->TupleDataMax = 255;
  680. tuple->TupleOffset = 0;
  681. /* Read the station address from tuple 0x90, subtuple 0x04 */
  682. tuple->DesiredTuple = 0x90;
  683. i = pcmcia_get_first_tuple(link, tuple);
  684. while (i == 0) {
  685. i = pcmcia_get_tuple_data(link, tuple);
  686. if ((i != 0) || (buf[0] == 0x04))
  687. break;
  688. i = pcmcia_get_next_tuple(link, tuple);
  689. }
  690. if (i != 0) {
  691. rc = -1;
  692. goto free_cfg_mem;
  693. }
  694. for (i = 0; i < 6; i++)
  695. dev->dev_addr[i] = buf[i+2];
  696. if (((manfid == MANFID_OSITECH) &&
  697. (cardid == PRODID_OSITECH_SEVEN)) ||
  698. ((manfid == MANFID_PSION) &&
  699. (cardid == PRODID_PSION_NET100))) {
  700. rc = osi_load_firmware(link);
  701. if (rc)
  702. goto free_cfg_mem;
  703. } else if (manfid == MANFID_OSITECH) {
  704. /* Make sure both functions are powered up */
  705. set_bits(0x300, link->io.BasePort1 + OSITECH_AUI_PWR);
  706. /* Now, turn on the interrupt for both card functions */
  707. set_bits(0x300, link->io.BasePort1 + OSITECH_RESET_ISR);
  708. DEBUG(2, "AUI/PWR: %4.4x RESET/ISR: %4.4x\n",
  709. inw(link->io.BasePort1 + OSITECH_AUI_PWR),
  710. inw(link->io.BasePort1 + OSITECH_RESET_ISR));
  711. }
  712. rc = 0;
  713. free_cfg_mem:
  714. kfree(cfg_mem);
  715. return rc;
  716. }
  717. static int smc91c92_suspend(struct pcmcia_device *link)
  718. {
  719. struct net_device *dev = link->priv;
  720. if (link->open)
  721. netif_device_detach(dev);
  722. return 0;
  723. }
  724. static int smc91c92_resume(struct pcmcia_device *link)
  725. {
  726. struct net_device *dev = link->priv;
  727. struct smc_private *smc = netdev_priv(dev);
  728. int i;
  729. if ((smc->manfid == MANFID_MEGAHERTZ) &&
  730. (smc->cardid == PRODID_MEGAHERTZ_EM3288))
  731. mhz_3288_power(link);
  732. if (smc->manfid == MANFID_MOTOROLA)
  733. mot_config(link);
  734. if ((smc->manfid == MANFID_OSITECH) &&
  735. (smc->cardid != PRODID_OSITECH_SEVEN)) {
  736. /* Power up the card and enable interrupts */
  737. set_bits(0x0300, dev->base_addr-0x10+OSITECH_AUI_PWR);
  738. set_bits(0x0300, dev->base_addr-0x10+OSITECH_RESET_ISR);
  739. }
  740. if (((smc->manfid == MANFID_OSITECH) &&
  741. (smc->cardid == PRODID_OSITECH_SEVEN)) ||
  742. ((smc->manfid == MANFID_PSION) &&
  743. (smc->cardid == PRODID_PSION_NET100))) {
  744. i = osi_load_firmware(link);
  745. if (i) {
  746. pr_err("smc91c92_cs: Failed to load firmware\n");
  747. return i;
  748. }
  749. }
  750. if (link->open) {
  751. smc_reset(dev);
  752. netif_device_attach(dev);
  753. }
  754. return 0;
  755. }
  756. /*======================================================================
  757. This verifies that the chip is some SMC91cXX variant, and returns
  758. the revision code if successful. Otherwise, it returns -ENODEV.
  759. ======================================================================*/
  760. static int check_sig(struct pcmcia_device *link)
  761. {
  762. struct net_device *dev = link->priv;
  763. unsigned int ioaddr = dev->base_addr;
  764. int width;
  765. u_short s;
  766. SMC_SELECT_BANK(1);
  767. if (inw(ioaddr + BANK_SELECT) >> 8 != 0x33) {
  768. /* Try powering up the chip */
  769. outw(0, ioaddr + CONTROL);
  770. mdelay(55);
  771. }
  772. /* Try setting bus width */
  773. width = (link->io.Attributes1 == IO_DATA_PATH_WIDTH_AUTO);
  774. s = inb(ioaddr + CONFIG);
  775. if (width)
  776. s |= CFG_16BIT;
  777. else
  778. s &= ~CFG_16BIT;
  779. outb(s, ioaddr + CONFIG);
  780. /* Check Base Address Register to make sure bus width is OK */
  781. s = inw(ioaddr + BASE_ADDR);
  782. if ((inw(ioaddr + BANK_SELECT) >> 8 == 0x33) &&
  783. ((s >> 8) != (s & 0xff))) {
  784. SMC_SELECT_BANK(3);
  785. s = inw(ioaddr + REVISION);
  786. return (s & 0xff);
  787. }
  788. if (width) {
  789. modconf_t mod = {
  790. .Attributes = CONF_IO_CHANGE_WIDTH,
  791. };
  792. printk(KERN_INFO "smc91c92_cs: using 8-bit IO window.\n");
  793. smc91c92_suspend(link);
  794. pcmcia_modify_configuration(link, &mod);
  795. smc91c92_resume(link);
  796. return check_sig(link);
  797. }
  798. return -ENODEV;
  799. }
  800. /*======================================================================
  801. smc91c92_config() is scheduled to run after a CARD_INSERTION event
  802. is received, to configure the PCMCIA socket, and to make the
  803. ethernet device available to the system.
  804. ======================================================================*/
  805. #define CS_EXIT_TEST(ret, svc, label) \
  806. if (ret != 0) { \
  807. cs_error(link, svc, ret); \
  808. goto label; \
  809. }
  810. static int smc91c92_config(struct pcmcia_device *link)
  811. {
  812. struct net_device *dev = link->priv;
  813. struct smc_private *smc = netdev_priv(dev);
  814. char *name;
  815. int i, j, rev;
  816. unsigned int ioaddr;
  817. u_long mir;
  818. DEBUG(0, "smc91c92_config(0x%p)\n", link);
  819. smc->manfid = link->manf_id;
  820. smc->cardid = link->card_id;
  821. if ((smc->manfid == MANFID_OSITECH) &&
  822. (smc->cardid != PRODID_OSITECH_SEVEN)) {
  823. i = osi_config(link);
  824. } else if ((smc->manfid == MANFID_MOTOROLA) ||
  825. ((smc->manfid == MANFID_MEGAHERTZ) &&
  826. ((smc->cardid == PRODID_MEGAHERTZ_VARIOUS) ||
  827. (smc->cardid == PRODID_MEGAHERTZ_EM3288)))) {
  828. i = mhz_mfc_config(link);
  829. } else {
  830. i = smc_config(link);
  831. }
  832. CS_EXIT_TEST(i, RequestIO, config_failed);
  833. i = pcmcia_request_irq(link, &link->irq);
  834. CS_EXIT_TEST(i, RequestIRQ, config_failed);
  835. i = pcmcia_request_configuration(link, &link->conf);
  836. CS_EXIT_TEST(i, RequestConfiguration, config_failed);
  837. if (smc->manfid == MANFID_MOTOROLA)
  838. mot_config(link);
  839. dev->irq = link->irq.AssignedIRQ;
  840. if ((if_port >= 0) && (if_port <= 2))
  841. dev->if_port = if_port;
  842. else
  843. printk(KERN_NOTICE "smc91c92_cs: invalid if_port requested\n");
  844. switch (smc->manfid) {
  845. case MANFID_OSITECH:
  846. case MANFID_PSION:
  847. i = osi_setup(link, smc->manfid, smc->cardid); break;
  848. case MANFID_SMC:
  849. case MANFID_NEW_MEDIA:
  850. i = smc_setup(link); break;
  851. case 0x128: /* For broken Megahertz cards */
  852. case MANFID_MEGAHERTZ:
  853. i = mhz_setup(link); break;
  854. case MANFID_MOTOROLA:
  855. default: /* get the hw address from EEPROM */
  856. i = mot_setup(link); break;
  857. }
  858. if (i != 0) {
  859. printk(KERN_NOTICE "smc91c92_cs: Unable to find hardware address.\n");
  860. goto config_undo;
  861. }
  862. smc->duplex = 0;
  863. smc->rx_ovrn = 0;
  864. rev = check_sig(link);
  865. name = "???";
  866. if (rev > 0)
  867. switch (rev >> 4) {
  868. case 3: name = "92"; break;
  869. case 4: name = ((rev & 15) >= 6) ? "96" : "94"; break;
  870. case 5: name = "95"; break;
  871. case 7: name = "100"; break;
  872. case 8: name = "100-FD"; break;
  873. case 9: name = "110"; break;
  874. }
  875. ioaddr = dev->base_addr;
  876. if (rev > 0) {
  877. u_long mcr;
  878. SMC_SELECT_BANK(0);
  879. mir = inw(ioaddr + MEMINFO) & 0xff;
  880. if (mir == 0xff) mir++;
  881. /* Get scale factor for memory size */
  882. mcr = ((rev >> 4) > 3) ? inw(ioaddr + MEMCFG) : 0x0200;
  883. mir *= 128 * (1<<((mcr >> 9) & 7));
  884. SMC_SELECT_BANK(1);
  885. smc->cfg = inw(ioaddr + CONFIG) & ~CFG_AUI_SELECT;
  886. smc->cfg |= CFG_NO_WAIT | CFG_16BIT | CFG_STATIC;
  887. if (smc->manfid == MANFID_OSITECH)
  888. smc->cfg |= CFG_IRQ_SEL_1 | CFG_IRQ_SEL_0;
  889. if ((rev >> 4) >= 7)
  890. smc->cfg |= CFG_MII_SELECT;
  891. } else
  892. mir = 0;
  893. if (smc->cfg & CFG_MII_SELECT) {
  894. SMC_SELECT_BANK(3);
  895. for (i = 0; i < 32; i++) {
  896. j = mdio_read(dev, i, 1);
  897. if ((j != 0) && (j != 0xffff)) break;
  898. }
  899. smc->mii_if.phy_id = (i < 32) ? i : -1;
  900. SMC_SELECT_BANK(0);
  901. }
  902. link->dev_node = &smc->node;
  903. SET_NETDEV_DEV(dev, &handle_to_dev(link));
  904. if (register_netdev(dev) != 0) {
  905. printk(KERN_ERR "smc91c92_cs: register_netdev() failed\n");
  906. link->dev_node = NULL;
  907. goto config_undo;
  908. }
  909. strcpy(smc->node.dev_name, dev->name);
  910. printk(KERN_INFO "%s: smc91c%s rev %d: io %#3lx, irq %d, "
  911. "hw_addr %pM\n",
  912. dev->name, name, (rev & 0x0f), dev->base_addr, dev->irq,
  913. dev->dev_addr);
  914. if (rev > 0) {
  915. if (mir & 0x3ff)
  916. printk(KERN_INFO " %lu byte", mir);
  917. else
  918. printk(KERN_INFO " %lu kb", mir>>10);
  919. printk(" buffer, %s xcvr\n", (smc->cfg & CFG_MII_SELECT) ?
  920. "MII" : if_names[dev->if_port]);
  921. }
  922. if (smc->cfg & CFG_MII_SELECT) {
  923. if (smc->mii_if.phy_id != -1) {
  924. DEBUG(0, " MII transceiver at index %d, status %x.\n",
  925. smc->mii_if.phy_id, j);
  926. } else {
  927. printk(KERN_NOTICE " No MII transceivers found!\n");
  928. }
  929. }
  930. return 0;
  931. config_undo:
  932. unregister_netdev(dev);
  933. config_failed: /* CS_EXIT_TEST() calls jump to here... */
  934. smc91c92_release(link);
  935. return -ENODEV;
  936. } /* smc91c92_config */
  937. /*======================================================================
  938. After a card is removed, smc91c92_release() will unregister the net
  939. device, and release the PCMCIA configuration. If the device is
  940. still open, this will be postponed until it is closed.
  941. ======================================================================*/
  942. static void smc91c92_release(struct pcmcia_device *link)
  943. {
  944. DEBUG(0, "smc91c92_release(0x%p)\n", link);
  945. if (link->win) {
  946. struct net_device *dev = link->priv;
  947. struct smc_private *smc = netdev_priv(dev);
  948. iounmap(smc->base);
  949. }
  950. pcmcia_disable_device(link);
  951. }
  952. /*======================================================================
  953. MII interface support for SMC91cXX based cards
  954. ======================================================================*/
  955. #define MDIO_SHIFT_CLK 0x04
  956. #define MDIO_DATA_OUT 0x01
  957. #define MDIO_DIR_WRITE 0x08
  958. #define MDIO_DATA_WRITE0 (MDIO_DIR_WRITE)
  959. #define MDIO_DATA_WRITE1 (MDIO_DIR_WRITE | MDIO_DATA_OUT)
  960. #define MDIO_DATA_READ 0x02
  961. static void mdio_sync(unsigned int addr)
  962. {
  963. int bits;
  964. for (bits = 0; bits < 32; bits++) {
  965. outb(MDIO_DATA_WRITE1, addr);
  966. outb(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
  967. }
  968. }
  969. static int mdio_read(struct net_device *dev, int phy_id, int loc)
  970. {
  971. unsigned int addr = dev->base_addr + MGMT;
  972. u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
  973. int i, retval = 0;
  974. mdio_sync(addr);
  975. for (i = 13; i >= 0; i--) {
  976. int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
  977. outb(dat, addr);
  978. outb(dat | MDIO_SHIFT_CLK, addr);
  979. }
  980. for (i = 19; i > 0; i--) {
  981. outb(0, addr);
  982. retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
  983. outb(MDIO_SHIFT_CLK, addr);
  984. }
  985. return (retval>>1) & 0xffff;
  986. }
  987. static void mdio_write(struct net_device *dev, int phy_id, int loc, int value)
  988. {
  989. unsigned int addr = dev->base_addr + MGMT;
  990. u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
  991. int i;
  992. mdio_sync(addr);
  993. for (i = 31; i >= 0; i--) {
  994. int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
  995. outb(dat, addr);
  996. outb(dat | MDIO_SHIFT_CLK, addr);
  997. }
  998. for (i = 1; i >= 0; i--) {
  999. outb(0, addr);
  1000. outb(MDIO_SHIFT_CLK, addr);
  1001. }
  1002. }
  1003. /*======================================================================
  1004. The driver core code, most of which should be common with a
  1005. non-PCMCIA implementation.
  1006. ======================================================================*/
  1007. #ifdef PCMCIA_DEBUG
  1008. static void smc_dump(struct net_device *dev)
  1009. {
  1010. unsigned int ioaddr = dev->base_addr;
  1011. u_short i, w, save;
  1012. save = inw(ioaddr + BANK_SELECT);
  1013. for (w = 0; w < 4; w++) {
  1014. SMC_SELECT_BANK(w);
  1015. printk(KERN_DEBUG "bank %d: ", w);
  1016. for (i = 0; i < 14; i += 2)
  1017. printk(" %04x", inw(ioaddr + i));
  1018. printk("\n");
  1019. }
  1020. outw(save, ioaddr + BANK_SELECT);
  1021. }
  1022. #endif
  1023. static int smc_open(struct net_device *dev)
  1024. {
  1025. struct smc_private *smc = netdev_priv(dev);
  1026. struct pcmcia_device *link = smc->p_dev;
  1027. #ifdef PCMCIA_DEBUG
  1028. DEBUG(0, "%s: smc_open(%p), ID/Window %4.4x.\n",
  1029. dev->name, dev, inw(dev->base_addr + BANK_SELECT));
  1030. if (pc_debug > 1) smc_dump(dev);
  1031. #endif
  1032. /* Check that the PCMCIA card is still here. */
  1033. if (!pcmcia_dev_present(link))
  1034. return -ENODEV;
  1035. /* Physical device present signature. */
  1036. if (check_sig(link) < 0) {
  1037. printk("smc91c92_cs: Yikes! Bad chip signature!\n");
  1038. return -ENODEV;
  1039. }
  1040. link->open++;
  1041. netif_start_queue(dev);
  1042. smc->saved_skb = NULL;
  1043. smc->packets_waiting = 0;
  1044. smc_reset(dev);
  1045. init_timer(&smc->media);
  1046. smc->media.function = &media_check;
  1047. smc->media.data = (u_long) dev;
  1048. smc->media.expires = jiffies + HZ;
  1049. add_timer(&smc->media);
  1050. return 0;
  1051. } /* smc_open */
  1052. /*====================================================================*/
  1053. static int smc_close(struct net_device *dev)
  1054. {
  1055. struct smc_private *smc = netdev_priv(dev);
  1056. struct pcmcia_device *link = smc->p_dev;
  1057. unsigned int ioaddr = dev->base_addr;
  1058. DEBUG(0, "%s: smc_close(), status %4.4x.\n",
  1059. dev->name, inw(ioaddr + BANK_SELECT));
  1060. netif_stop_queue(dev);
  1061. /* Shut off all interrupts, and turn off the Tx and Rx sections.
  1062. Don't bother to check for chip present. */
  1063. SMC_SELECT_BANK(2); /* Nominally paranoia, but do no assume... */
  1064. outw(0, ioaddr + INTERRUPT);
  1065. SMC_SELECT_BANK(0);
  1066. mask_bits(0xff00, ioaddr + RCR);
  1067. mask_bits(0xff00, ioaddr + TCR);
  1068. /* Put the chip into power-down mode. */
  1069. SMC_SELECT_BANK(1);
  1070. outw(CTL_POWERDOWN, ioaddr + CONTROL );
  1071. link->open--;
  1072. del_timer_sync(&smc->media);
  1073. return 0;
  1074. } /* smc_close */
  1075. /*======================================================================
  1076. Transfer a packet to the hardware and trigger the packet send.
  1077. This may be called at either from either the Tx queue code
  1078. or the interrupt handler.
  1079. ======================================================================*/
  1080. static void smc_hardware_send_packet(struct net_device * dev)
  1081. {
  1082. struct smc_private *smc = netdev_priv(dev);
  1083. struct sk_buff *skb = smc->saved_skb;
  1084. unsigned int ioaddr = dev->base_addr;
  1085. u_char packet_no;
  1086. if (!skb) {
  1087. printk(KERN_ERR "%s: In XMIT with no packet to send.\n", dev->name);
  1088. return;
  1089. }
  1090. /* There should be a packet slot waiting. */
  1091. packet_no = inw(ioaddr + PNR_ARR) >> 8;
  1092. if (packet_no & 0x80) {
  1093. /* If not, there is a hardware problem! Likely an ejected card. */
  1094. printk(KERN_WARNING "%s: 91c92 hardware Tx buffer allocation"
  1095. " failed, status %#2.2x.\n", dev->name, packet_no);
  1096. dev_kfree_skb_irq(skb);
  1097. smc->saved_skb = NULL;
  1098. netif_start_queue(dev);
  1099. return;
  1100. }
  1101. dev->stats.tx_bytes += skb->len;
  1102. /* The card should use the just-allocated buffer. */
  1103. outw(packet_no, ioaddr + PNR_ARR);
  1104. /* point to the beginning of the packet */
  1105. outw(PTR_AUTOINC , ioaddr + POINTER);
  1106. /* Send the packet length (+6 for status, length and ctl byte)
  1107. and the status word (set to zeros). */
  1108. {
  1109. u_char *buf = skb->data;
  1110. u_int length = skb->len; /* The chip will pad to ethernet min. */
  1111. DEBUG(2, "%s: Trying to xmit packet of length %d.\n",
  1112. dev->name, length);
  1113. /* send the packet length: +6 for status word, length, and ctl */
  1114. outw(0, ioaddr + DATA_1);
  1115. outw(length + 6, ioaddr + DATA_1);
  1116. outsw(ioaddr + DATA_1, buf, length >> 1);
  1117. /* The odd last byte, if there is one, goes in the control word. */
  1118. outw((length & 1) ? 0x2000 | buf[length-1] : 0, ioaddr + DATA_1);
  1119. }
  1120. /* Enable the Tx interrupts, both Tx (TxErr) and TxEmpty. */
  1121. outw(((IM_TX_INT|IM_TX_EMPTY_INT)<<8) |
  1122. (inw(ioaddr + INTERRUPT) & 0xff00),
  1123. ioaddr + INTERRUPT);
  1124. /* The chip does the rest of the work. */
  1125. outw(MC_ENQUEUE , ioaddr + MMU_CMD);
  1126. smc->saved_skb = NULL;
  1127. dev_kfree_skb_irq(skb);
  1128. dev->trans_start = jiffies;
  1129. netif_start_queue(dev);
  1130. return;
  1131. }
  1132. /*====================================================================*/
  1133. static void smc_tx_timeout(struct net_device *dev)
  1134. {
  1135. struct smc_private *smc = netdev_priv(dev);
  1136. unsigned int ioaddr = dev->base_addr;
  1137. printk(KERN_NOTICE "%s: SMC91c92 transmit timed out, "
  1138. "Tx_status %2.2x status %4.4x.\n",
  1139. dev->name, inw(ioaddr)&0xff, inw(ioaddr + 2));
  1140. dev->stats.tx_errors++;
  1141. smc_reset(dev);
  1142. dev->trans_start = jiffies;
  1143. smc->saved_skb = NULL;
  1144. netif_wake_queue(dev);
  1145. }
  1146. static int smc_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1147. {
  1148. struct smc_private *smc = netdev_priv(dev);
  1149. unsigned int ioaddr = dev->base_addr;
  1150. u_short num_pages;
  1151. short time_out, ir;
  1152. unsigned long flags;
  1153. netif_stop_queue(dev);
  1154. DEBUG(2, "%s: smc_start_xmit(length = %d) called,"
  1155. " status %4.4x.\n", dev->name, skb->len, inw(ioaddr + 2));
  1156. if (smc->saved_skb) {
  1157. /* THIS SHOULD NEVER HAPPEN. */
  1158. dev->stats.tx_aborted_errors++;
  1159. printk(KERN_DEBUG "%s: Internal error -- sent packet while busy.\n",
  1160. dev->name);
  1161. return NETDEV_TX_BUSY;
  1162. }
  1163. smc->saved_skb = skb;
  1164. num_pages = skb->len >> 8;
  1165. if (num_pages > 7) {
  1166. printk(KERN_ERR "%s: Far too big packet error.\n", dev->name);
  1167. dev_kfree_skb (skb);
  1168. smc->saved_skb = NULL;
  1169. dev->stats.tx_dropped++;
  1170. return 0; /* Do not re-queue this packet. */
  1171. }
  1172. /* A packet is now waiting. */
  1173. smc->packets_waiting++;
  1174. spin_lock_irqsave(&smc->lock, flags);
  1175. SMC_SELECT_BANK(2); /* Paranoia, we should always be in window 2 */
  1176. /* need MC_RESET to keep the memory consistent. errata? */
  1177. if (smc->rx_ovrn) {
  1178. outw(MC_RESET, ioaddr + MMU_CMD);
  1179. smc->rx_ovrn = 0;
  1180. }
  1181. /* Allocate the memory; send the packet now if we win. */
  1182. outw(MC_ALLOC | num_pages, ioaddr + MMU_CMD);
  1183. for (time_out = MEMORY_WAIT_TIME; time_out >= 0; time_out--) {
  1184. ir = inw(ioaddr+INTERRUPT);
  1185. if (ir & IM_ALLOC_INT) {
  1186. /* Acknowledge the interrupt, send the packet. */
  1187. outw((ir&0xff00) | IM_ALLOC_INT, ioaddr + INTERRUPT);
  1188. smc_hardware_send_packet(dev); /* Send the packet now.. */
  1189. spin_unlock_irqrestore(&smc->lock, flags);
  1190. return 0;
  1191. }
  1192. }
  1193. /* Otherwise defer until the Tx-space-allocated interrupt. */
  1194. DEBUG(2, "%s: memory allocation deferred.\n", dev->name);
  1195. outw((IM_ALLOC_INT << 8) | (ir & 0xff00), ioaddr + INTERRUPT);
  1196. spin_unlock_irqrestore(&smc->lock, flags);
  1197. return 0;
  1198. }
  1199. /*======================================================================
  1200. Handle a Tx anomolous event. Entered while in Window 2.
  1201. ======================================================================*/
  1202. static void smc_tx_err(struct net_device * dev)
  1203. {
  1204. struct smc_private *smc = netdev_priv(dev);
  1205. unsigned int ioaddr = dev->base_addr;
  1206. int saved_packet = inw(ioaddr + PNR_ARR) & 0xff;
  1207. int packet_no = inw(ioaddr + FIFO_PORTS) & 0x7f;
  1208. int tx_status;
  1209. /* select this as the packet to read from */
  1210. outw(packet_no, ioaddr + PNR_ARR);
  1211. /* read the first word from this packet */
  1212. outw(PTR_AUTOINC | PTR_READ | 0, ioaddr + POINTER);
  1213. tx_status = inw(ioaddr + DATA_1);
  1214. dev->stats.tx_errors++;
  1215. if (tx_status & TS_LOSTCAR) dev->stats.tx_carrier_errors++;
  1216. if (tx_status & TS_LATCOL) dev->stats.tx_window_errors++;
  1217. if (tx_status & TS_16COL) {
  1218. dev->stats.tx_aborted_errors++;
  1219. smc->tx_err++;
  1220. }
  1221. if (tx_status & TS_SUCCESS) {
  1222. printk(KERN_NOTICE "%s: Successful packet caused error "
  1223. "interrupt?\n", dev->name);
  1224. }
  1225. /* re-enable transmit */
  1226. SMC_SELECT_BANK(0);
  1227. outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
  1228. SMC_SELECT_BANK(2);
  1229. outw(MC_FREEPKT, ioaddr + MMU_CMD); /* Free the packet memory. */
  1230. /* one less packet waiting for me */
  1231. smc->packets_waiting--;
  1232. outw(saved_packet, ioaddr + PNR_ARR);
  1233. return;
  1234. }
  1235. /*====================================================================*/
  1236. static void smc_eph_irq(struct net_device *dev)
  1237. {
  1238. struct smc_private *smc = netdev_priv(dev);
  1239. unsigned int ioaddr = dev->base_addr;
  1240. u_short card_stats, ephs;
  1241. SMC_SELECT_BANK(0);
  1242. ephs = inw(ioaddr + EPH);
  1243. DEBUG(2, "%s: Ethernet protocol handler interrupt, status"
  1244. " %4.4x.\n", dev->name, ephs);
  1245. /* Could be a counter roll-over warning: update stats. */
  1246. card_stats = inw(ioaddr + COUNTER);
  1247. /* single collisions */
  1248. dev->stats.collisions += card_stats & 0xF;
  1249. card_stats >>= 4;
  1250. /* multiple collisions */
  1251. dev->stats.collisions += card_stats & 0xF;
  1252. #if 0 /* These are for when linux supports these statistics */
  1253. card_stats >>= 4; /* deferred */
  1254. card_stats >>= 4; /* excess deferred */
  1255. #endif
  1256. /* If we had a transmit error we must re-enable the transmitter. */
  1257. outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
  1258. /* Clear a link error interrupt. */
  1259. SMC_SELECT_BANK(1);
  1260. outw(CTL_AUTO_RELEASE | 0x0000, ioaddr + CONTROL);
  1261. outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
  1262. ioaddr + CONTROL);
  1263. SMC_SELECT_BANK(2);
  1264. }
  1265. /*====================================================================*/
  1266. static irqreturn_t smc_interrupt(int irq, void *dev_id)
  1267. {
  1268. struct net_device *dev = dev_id;
  1269. struct smc_private *smc = netdev_priv(dev);
  1270. unsigned int ioaddr;
  1271. u_short saved_bank, saved_pointer, mask, status;
  1272. unsigned int handled = 1;
  1273. char bogus_cnt = INTR_WORK; /* Work we are willing to do. */
  1274. if (!netif_device_present(dev))
  1275. return IRQ_NONE;
  1276. ioaddr = dev->base_addr;
  1277. DEBUG(3, "%s: SMC91c92 interrupt %d at %#x.\n", dev->name,
  1278. irq, ioaddr);
  1279. spin_lock(&smc->lock);
  1280. smc->watchdog = 0;
  1281. saved_bank = inw(ioaddr + BANK_SELECT);
  1282. if ((saved_bank & 0xff00) != 0x3300) {
  1283. /* The device does not exist -- the card could be off-line, or
  1284. maybe it has been ejected. */
  1285. DEBUG(1, "%s: SMC91c92 interrupt %d for non-existent"
  1286. "/ejected device.\n", dev->name, irq);
  1287. handled = 0;
  1288. goto irq_done;
  1289. }
  1290. SMC_SELECT_BANK(2);
  1291. saved_pointer = inw(ioaddr + POINTER);
  1292. mask = inw(ioaddr + INTERRUPT) >> 8;
  1293. /* clear all interrupts */
  1294. outw(0, ioaddr + INTERRUPT);
  1295. do { /* read the status flag, and mask it */
  1296. status = inw(ioaddr + INTERRUPT) & 0xff;
  1297. DEBUG(3, "%s: Status is %#2.2x (mask %#2.2x).\n", dev->name,
  1298. status, mask);
  1299. if ((status & mask) == 0) {
  1300. if (bogus_cnt == INTR_WORK)
  1301. handled = 0;
  1302. break;
  1303. }
  1304. if (status & IM_RCV_INT) {
  1305. /* Got a packet(s). */
  1306. smc_rx(dev);
  1307. }
  1308. if (status & IM_TX_INT) {
  1309. smc_tx_err(dev);
  1310. outw(IM_TX_INT, ioaddr + INTERRUPT);
  1311. }
  1312. status &= mask;
  1313. if (status & IM_TX_EMPTY_INT) {
  1314. outw(IM_TX_EMPTY_INT, ioaddr + INTERRUPT);
  1315. mask &= ~IM_TX_EMPTY_INT;
  1316. dev->stats.tx_packets += smc->packets_waiting;
  1317. smc->packets_waiting = 0;
  1318. }
  1319. if (status & IM_ALLOC_INT) {
  1320. /* Clear this interrupt so it doesn't happen again */
  1321. mask &= ~IM_ALLOC_INT;
  1322. smc_hardware_send_packet(dev);
  1323. /* enable xmit interrupts based on this */
  1324. mask |= (IM_TX_EMPTY_INT | IM_TX_INT);
  1325. /* and let the card send more packets to me */
  1326. netif_wake_queue(dev);
  1327. }
  1328. if (status & IM_RX_OVRN_INT) {
  1329. dev->stats.rx_errors++;
  1330. dev->stats.rx_fifo_errors++;
  1331. if (smc->duplex)
  1332. smc->rx_ovrn = 1; /* need MC_RESET outside smc_interrupt */
  1333. outw(IM_RX_OVRN_INT, ioaddr + INTERRUPT);
  1334. }
  1335. if (status & IM_EPH_INT)
  1336. smc_eph_irq(dev);
  1337. } while (--bogus_cnt);
  1338. DEBUG(3, " Restoring saved registers mask %2.2x bank %4.4x"
  1339. " pointer %4.4x.\n", mask, saved_bank, saved_pointer);
  1340. /* restore state register */
  1341. outw((mask<<8), ioaddr + INTERRUPT);
  1342. outw(saved_pointer, ioaddr + POINTER);
  1343. SMC_SELECT_BANK(saved_bank);
  1344. DEBUG(3, "%s: Exiting interrupt IRQ%d.\n", dev->name, irq);
  1345. irq_done:
  1346. if ((smc->manfid == MANFID_OSITECH) &&
  1347. (smc->cardid != PRODID_OSITECH_SEVEN)) {
  1348. /* Retrigger interrupt if needed */
  1349. mask_bits(0x00ff, ioaddr-0x10+OSITECH_RESET_ISR);
  1350. set_bits(0x0300, ioaddr-0x10+OSITECH_RESET_ISR);
  1351. }
  1352. if (smc->manfid == MANFID_MOTOROLA) {
  1353. u_char cor;
  1354. cor = readb(smc->base + MOT_UART + CISREG_COR);
  1355. writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_UART + CISREG_COR);
  1356. writeb(cor, smc->base + MOT_UART + CISREG_COR);
  1357. cor = readb(smc->base + MOT_LAN + CISREG_COR);
  1358. writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_LAN + CISREG_COR);
  1359. writeb(cor, smc->base + MOT_LAN + CISREG_COR);
  1360. }
  1361. #ifdef DOES_NOT_WORK
  1362. if (smc->base != NULL) { /* Megahertz MFC's */
  1363. readb(smc->base+MEGAHERTZ_ISR);
  1364. readb(smc->base+MEGAHERTZ_ISR);
  1365. }
  1366. #endif
  1367. spin_unlock(&smc->lock);
  1368. return IRQ_RETVAL(handled);
  1369. }
  1370. /*====================================================================*/
  1371. static void smc_rx(struct net_device *dev)
  1372. {
  1373. unsigned int ioaddr = dev->base_addr;
  1374. int rx_status;
  1375. int packet_length; /* Caution: not frame length, rather words
  1376. to transfer from the chip. */
  1377. /* Assertion: we are in Window 2. */
  1378. if (inw(ioaddr + FIFO_PORTS) & FP_RXEMPTY) {
  1379. printk(KERN_ERR "%s: smc_rx() with nothing on Rx FIFO.\n",
  1380. dev->name);
  1381. return;
  1382. }
  1383. /* Reset the read pointer, and read the status and packet length. */
  1384. outw(PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER);
  1385. rx_status = inw(ioaddr + DATA_1);
  1386. packet_length = inw(ioaddr + DATA_1) & 0x07ff;
  1387. DEBUG(2, "%s: Receive status %4.4x length %d.\n",
  1388. dev->name, rx_status, packet_length);
  1389. if (!(rx_status & RS_ERRORS)) {
  1390. /* do stuff to make a new packet */
  1391. struct sk_buff *skb;
  1392. /* Note: packet_length adds 5 or 6 extra bytes here! */
  1393. skb = dev_alloc_skb(packet_length+2);
  1394. if (skb == NULL) {
  1395. DEBUG(1, "%s: Low memory, packet dropped.\n", dev->name);
  1396. dev->stats.rx_dropped++;
  1397. outw(MC_RELEASE, ioaddr + MMU_CMD);
  1398. return;
  1399. }
  1400. packet_length -= (rx_status & RS_ODDFRAME ? 5 : 6);
  1401. skb_reserve(skb, 2);
  1402. insw(ioaddr+DATA_1, skb_put(skb, packet_length),
  1403. (packet_length+1)>>1);
  1404. skb->protocol = eth_type_trans(skb, dev);
  1405. netif_rx(skb);
  1406. dev->last_rx = jiffies;
  1407. dev->stats.rx_packets++;
  1408. dev->stats.rx_bytes += packet_length;
  1409. if (rx_status & RS_MULTICAST)
  1410. dev->stats.multicast++;
  1411. } else {
  1412. /* error ... */
  1413. dev->stats.rx_errors++;
  1414. if (rx_status & RS_ALGNERR) dev->stats.rx_frame_errors++;
  1415. if (rx_status & (RS_TOOSHORT | RS_TOOLONG))
  1416. dev->stats.rx_length_errors++;
  1417. if (rx_status & RS_BADCRC) dev->stats.rx_crc_errors++;
  1418. }
  1419. /* Let the MMU free the memory of this packet. */
  1420. outw(MC_RELEASE, ioaddr + MMU_CMD);
  1421. return;
  1422. }
  1423. /*======================================================================
  1424. Calculate values for the hardware multicast filter hash table.
  1425. ======================================================================*/
  1426. static void fill_multicast_tbl(int count, struct dev_mc_list *addrs,
  1427. u_char *multicast_table)
  1428. {
  1429. struct dev_mc_list *mc_addr;
  1430. for (mc_addr = addrs; mc_addr && count-- > 0; mc_addr = mc_addr->next) {
  1431. u_int position = ether_crc(6, mc_addr->dmi_addr);
  1432. #ifndef final_version /* Verify multicast address. */
  1433. if ((mc_addr->dmi_addr[0] & 1) == 0)
  1434. continue;
  1435. #endif
  1436. multicast_table[position >> 29] |= 1 << ((position >> 26) & 7);
  1437. }
  1438. }
  1439. /*======================================================================
  1440. Set the receive mode.
  1441. This routine is used by both the protocol level to notify us of
  1442. promiscuous/multicast mode changes, and by the open/reset code to
  1443. initialize the Rx registers. We always set the multicast list and
  1444. leave the receiver running.
  1445. ======================================================================*/
  1446. static void set_rx_mode(struct net_device *dev)
  1447. {
  1448. unsigned int ioaddr = dev->base_addr;
  1449. struct smc_private *smc = netdev_priv(dev);
  1450. u_int multicas