dm9000.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. * dm9000.c: Version 1.2 03/18/2003
  3. *
  4. * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
  5. * Copyright (C) 1997 Sten Wang
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
  18. *
  19. * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
  20. * 06/22/2001 Support DM9801 progrmming
  21. * E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
  22. * E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
  23. * R17 = (R17 & 0xfff0) | NF + 3
  24. * E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
  25. * R17 = (R17 & 0xfff0) | NF
  26. *
  27. * v1.00 modify by simon 2001.9.5
  28. * change for kernel 2.4.x
  29. *
  30. * v1.1 11/09/2001 fix force mode bug
  31. *
  32. * v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
  33. * Fixed phy reset.
  34. * Added tx/rx 32 bit mode.
  35. * Cleaned up for kernel merge.
  36. *
  37. * 03/03/2004 Sascha Hauer <s.hauer@pengutronix.de>
  38. * Port to 2.6 kernel
  39. *
  40. * 24-Sep-2004 Ben Dooks <ben@simtec.co.uk>
  41. * Cleanup of code to remove ifdefs
  42. * Allowed platform device data to influence access width
  43. * Reformatting areas of code
  44. *
  45. * 17-Mar-2005 Sascha Hauer <s.hauer@pengutronix.de>
  46. * * removed 2.4 style module parameters
  47. * * removed removed unused stat counter and fixed
  48. * net_device_stats
  49. * * introduced tx_timeout function
  50. * * reworked locking
  51. *
  52. * 01-Jul-2005 Ben Dooks <ben@simtec.co.uk>
  53. * * fixed spinlock call without pointer
  54. * * ensure spinlock is initialised
  55. */
  56. #include <linux/module.h>
  57. #include <linux/ioport.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/etherdevice.h>
  60. #include <linux/init.h>
  61. #include <linux/skbuff.h>
  62. #include <linux/spinlock.h>
  63. #include <linux/crc32.h>
  64. #include <linux/mii.h>
  65. #include <linux/dm9000.h>
  66. #include <linux/delay.h>
  67. #include <linux/platform_device.h>
  68. #include <asm/delay.h>
  69. #include <asm/irq.h>
  70. #include <asm/io.h>
  71. #include "dm9000.h"
  72. /* Board/System/Debug information/definition ---------------- */
  73. #define DM9000_PHY 0x40 /* PHY address 0x01 */
  74. #define TRUE 1
  75. #define FALSE 0
  76. #define CARDNAME "dm9000"
  77. #define PFX CARDNAME ": "
  78. #define DM9000_TIMER_WUT jiffies+(HZ*2) /* timer wakeup time : 2 second */
  79. #define DM9000_DEBUG 0
  80. #if DM9000_DEBUG > 2
  81. #define PRINTK3(args...) printk(CARDNAME ": " args)
  82. #else
  83. #define PRINTK3(args...) do { } while(0)
  84. #endif
  85. #if DM9000_DEBUG > 1
  86. #define PRINTK2(args...) printk(CARDNAME ": " args)
  87. #else
  88. #define PRINTK2(args...) do { } while(0)
  89. #endif
  90. #if DM9000_DEBUG > 0
  91. #define PRINTK1(args...) printk(CARDNAME ": " args)
  92. #define PRINTK(args...) printk(CARDNAME ": " args)
  93. #else
  94. #define PRINTK1(args...) do { } while(0)
  95. #define PRINTK(args...) printk(KERN_DEBUG args)
  96. #endif
  97. /*
  98. * Transmit timeout, default 5 seconds.
  99. */
  100. static int watchdog = 5000;
  101. module_param(watchdog, int, 0400);
  102. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
  103. /* Structure/enum declaration ------------------------------- */
  104. typedef struct board_info {
  105. void __iomem *io_addr; /* Register I/O base address */
  106. void __iomem *io_data; /* Data I/O address */
  107. u16 irq; /* IRQ */
  108. u16 tx_pkt_cnt;
  109. u16 queue_pkt_len;
  110. u16 queue_start_addr;
  111. u16 dbug_cnt;
  112. u8 io_mode; /* 0:word, 2:byte */
  113. u8 phy_addr;
  114. void (*inblk)(void __iomem *port, void *data, int length);
  115. void (*outblk)(void __iomem *port, void *data, int length);
  116. void (*dumpblk)(void __iomem *port, int length);
  117. struct resource *addr_res; /* resources found */
  118. struct resource *data_res;
  119. struct resource *addr_req; /* resources requested */
  120. struct resource *data_req;
  121. struct resource *irq_res;
  122. struct timer_list timer;
  123. struct net_device_stats stats;
  124. unsigned char srom[128];
  125. spinlock_t lock;
  126. struct mii_if_info mii;
  127. u32 msg_enable;
  128. } board_info_t;
  129. /* function declaration ------------------------------------- */
  130. static int dm9000_probe(struct platform_device *);
  131. static int dm9000_open(struct net_device *);
  132. static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
  133. static int dm9000_stop(struct net_device *);
  134. static void dm9000_timer(unsigned long);
  135. static void dm9000_init_dm9000(struct net_device *);
  136. static struct net_device_stats *dm9000_get_stats(struct net_device *);
  137. static irqreturn_t dm9000_interrupt(int, void *, struct pt_regs *);
  138. static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
  139. static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
  140. int value);
  141. static u16 read_srom_word(board_info_t *, int);
  142. static void dm9000_rx(struct net_device *);
  143. static void dm9000_hash_table(struct net_device *);
  144. //#define DM9000_PROGRAM_EEPROM
  145. #ifdef DM9000_PROGRAM_EEPROM
  146. static void program_eeprom(board_info_t * db);
  147. #endif
  148. /* DM9000 network board routine ---------------------------- */
  149. static void
  150. dm9000_reset(board_info_t * db)
  151. {
  152. PRINTK1("dm9000x: resetting\n");
  153. /* RESET device */
  154. writeb(DM9000_NCR, db->io_addr);
  155. udelay(200);
  156. writeb(NCR_RST, db->io_data);
  157. udelay(200);
  158. }
  159. /*
  160. * Read a byte from I/O port
  161. */
  162. static u8
  163. ior(board_info_t * db, int reg)
  164. {
  165. writeb(reg, db->io_addr);
  166. return readb(db->io_data);
  167. }
  168. /*
  169. * Write a byte to I/O port
  170. */
  171. static void
  172. iow(board_info_t * db, int reg, int value)
  173. {
  174. writeb(reg, db->io_addr);
  175. writeb(value, db->io_data);
  176. }
  177. /* routines for sending block to chip */
  178. static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
  179. {
  180. writesb(reg, data, count);
  181. }
  182. static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
  183. {
  184. writesw(reg, data, (count+1) >> 1);
  185. }
  186. static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
  187. {
  188. writesl(reg, data, (count+3) >> 2);
  189. }
  190. /* input block from chip to memory */
  191. static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
  192. {
  193. readsb(reg, data, count);
  194. }
  195. static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
  196. {
  197. readsw(reg, data, (count+1) >> 1);
  198. }
  199. static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
  200. {
  201. readsl(reg, data, (count+3) >> 2);
  202. }
  203. /* dump block from chip to null */
  204. static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
  205. {
  206. int i;
  207. int tmp;
  208. for (i = 0; i < count; i++)
  209. tmp = readb(reg);
  210. }
  211. static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
  212. {
  213. int i;
  214. int tmp;
  215. count = (count + 1) >> 1;
  216. for (i = 0; i < count; i++)
  217. tmp = readw(reg);
  218. }
  219. static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
  220. {
  221. int i;
  222. int tmp;
  223. count = (count + 3) >> 2;
  224. for (i = 0; i < count; i++)
  225. tmp = readl(reg);
  226. }
  227. /* dm9000_set_io
  228. *
  229. * select the specified set of io routines to use with the
  230. * device
  231. */
  232. static void dm9000_set_io(struct board_info *db, int byte_width)
  233. {
  234. /* use the size of the data resource to work out what IO
  235. * routines we want to use
  236. */
  237. switch (byte_width) {
  238. case 1:
  239. db->dumpblk = dm9000_dumpblk_8bit;
  240. db->outblk = dm9000_outblk_8bit;
  241. db->inblk = dm9000_inblk_8bit;
  242. break;
  243. case 2:
  244. db->dumpblk = dm9000_dumpblk_16bit;
  245. db->outblk = dm9000_outblk_16bit;
  246. db->inblk = dm9000_inblk_16bit;
  247. break;
  248. case 3:
  249. printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
  250. db->dumpblk = dm9000_dumpblk_16bit;
  251. db->outblk = dm9000_outblk_16bit;
  252. db->inblk = dm9000_inblk_16bit;
  253. break;
  254. case 4:
  255. default:
  256. db->dumpblk = dm9000_dumpblk_32bit;
  257. db->outblk = dm9000_outblk_32bit;
  258. db->inblk = dm9000_inblk_32bit;
  259. break;
  260. }
  261. }
  262. /* Our watchdog timed out. Called by the networking layer */
  263. static void dm9000_timeout(struct net_device *dev)
  264. {
  265. board_info_t *db = (board_info_t *) dev->priv;
  266. u8 reg_save;
  267. unsigned long flags;
  268. /* Save previous register address */
  269. reg_save = readb(db->io_addr);
  270. spin_lock_irqsave(&db->lock,flags);
  271. netif_stop_queue(dev);
  272. dm9000_reset(db);
  273. dm9000_init_dm9000(dev);
  274. /* We can accept TX packets again */
  275. dev->trans_start = jiffies;
  276. netif_wake_queue(dev);
  277. /* Restore previous register address */
  278. writeb(reg_save, db->io_addr);
  279. spin_unlock_irqrestore(&db->lock,flags);
  280. }
  281. /* dm9000_release_board
  282. *
  283. * release a board, and any mapped resources
  284. */
  285. static void
  286. dm9000_release_board(struct platform_device *pdev, struct board_info *db)
  287. {
  288. if (db->data_res == NULL) {
  289. if (db->addr_res != NULL)
  290. release_mem_region((unsigned long)db->io_addr, 4);
  291. return;
  292. }
  293. /* unmap our resources */
  294. iounmap(db->io_addr);
  295. iounmap(db->io_data);
  296. /* release the resources */
  297. if (db->data_req != NULL) {
  298. release_resource(db->data_req);
  299. kfree(db->data_req);
  300. }
  301. if (db->addr_res != NULL) {
  302. release_resource(db->addr_res);
  303. kfree(db->addr_req);
  304. }
  305. }
  306. #define res_size(_r) (((_r)->end - (_r)->start) + 1)
  307. /*
  308. * Search DM9000 board, allocate space and register it
  309. */
  310. static int
  311. dm9000_probe(struct platform_device *pdev)
  312. {
  313. struct dm9000_plat_data *pdata = pdev->dev.platform_data;
  314. struct board_info *db; /* Point a board information structure */
  315. struct net_device *ndev;
  316. unsigned long base;
  317. int ret = 0;
  318. int iosize;
  319. int i;
  320. u32 id_val;
  321. /* Init network device */
  322. ndev = alloc_etherdev(sizeof (struct board_info));
  323. if (!ndev) {
  324. printk("%s: could not allocate device.\n", CARDNAME);
  325. return -ENOMEM;
  326. }
  327. SET_MODULE_OWNER(ndev);
  328. SET_NETDEV_DEV(ndev, &pdev->dev);
  329. PRINTK2("dm9000_probe()");
  330. /* setup board info structure */
  331. db = (struct board_info *) ndev->priv;
  332. memset(db, 0, sizeof (*db));
  333. spin_lock_init(&db->lock);
  334. if (pdev->num_resources < 2) {
  335. ret = -ENODEV;
  336. goto out;
  337. } else if (pdev->num_resources == 2) {
  338. base = pdev->resource[0].start;
  339. if (!request_mem_region(base, 4, ndev->name)) {
  340. ret = -EBUSY;
  341. goto out;
  342. }
  343. ndev->base_addr = base;
  344. ndev->irq = pdev->resource[1].start;
  345. db->io_addr = (void __iomem *)base;
  346. db->io_data = (void __iomem *)(base + 4);
  347. } else {
  348. db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  349. db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  350. db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  351. if (db->addr_res == NULL || db->data_res == NULL ||
  352. db->irq_res == NULL) {
  353. printk(KERN_ERR PFX "insufficient resources\n");
  354. ret = -ENOENT;
  355. goto out;
  356. }
  357. i = res_size(db->addr_res);
  358. db->addr_req = request_mem_region(db->addr_res->start, i,
  359. pdev->name);
  360. if (db->addr_req == NULL) {
  361. printk(KERN_ERR PFX "cannot claim address reg area\n");
  362. ret = -EIO;
  363. goto out;
  364. }
  365. db->io_addr = ioremap(db->addr_res->start, i);
  366. if (db->io_addr == NULL) {
  367. printk(KERN_ERR "failed to ioremap address reg\n");
  368. ret = -EINVAL;
  369. goto out;
  370. }
  371. iosize = res_size(db->data_res);
  372. db->data_req = request_mem_region(db->data_res->start, iosize,
  373. pdev->name);
  374. if (db->data_req == NULL) {
  375. printk(KERN_ERR PFX "cannot claim data reg area\n");
  376. ret = -EIO;
  377. goto out;
  378. }
  379. db->io_data = ioremap(db->data_res->start, iosize);
  380. if (db->io_data == NULL) {
  381. printk(KERN_ERR "failed to ioremap data reg\n");
  382. ret = -EINVAL;
  383. goto out;
  384. }
  385. /* fill in parameters for net-dev structure */
  386. ndev->base_addr = (unsigned long)db->io_addr;
  387. ndev->irq = db->irq_res->start;
  388. /* ensure at least we have a default set of IO routines */
  389. dm9000_set_io(db, iosize);
  390. }
  391. /* check to see if anything is being over-ridden */
  392. if (pdata != NULL) {
  393. /* check to see if the driver wants to over-ride the
  394. * default IO width */
  395. if (pdata->flags & DM9000_PLATF_8BITONLY)
  396. dm9000_set_io(db, 1);
  397. if (pdata->flags & DM9000_PLATF_16BITONLY)
  398. dm9000_set_io(db, 2);
  399. if (pdata->flags & DM9000_PLATF_32BITONLY)
  400. dm9000_set_io(db, 4);
  401. /* check to see if there are any IO routine
  402. * over-rides */
  403. if (pdata->inblk != NULL)
  404. db->inblk = pdata->inblk;
  405. if (pdata->outblk != NULL)
  406. db->outblk = pdata->outblk;
  407. if (pdata->dumpblk != NULL)
  408. db->dumpblk = pdata->dumpblk;
  409. }
  410. dm9000_reset(db);
  411. /* try two times, DM9000 sometimes gets the first read wrong */
  412. for (i = 0; i < 2; i++) {
  413. id_val = ior(db, DM9000_VIDL);
  414. id_val |= (u32)ior(db, DM9000_VIDH) << 8;
  415. id_val |= (u32)ior(db, DM9000_PIDL) << 16;
  416. id_val |= (u32)ior(db, DM9000_PIDH) << 24;
  417. if (id_val == DM9000_ID)
  418. break;
  419. printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
  420. }
  421. if (id_val != DM9000_ID) {
  422. printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
  423. goto release;
  424. }
  425. /* from this point we assume that we have found a DM9000 */
  426. /* driver system function */
  427. ether_setup(ndev);
  428. ndev->open = &dm9000_open;
  429. ndev->hard_start_xmit = &dm9000_start_xmit;
  430. ndev->tx_timeout = &dm9000_timeout;
  431. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  432. ndev->stop = &dm9000_stop;
  433. ndev->get_stats = &dm9000_get_stats;
  434. ndev->set_multicast_list = &dm9000_hash_table;
  435. #ifdef DM9000_PROGRAM_EEPROM
  436. program_eeprom(db);
  437. #endif
  438. db->msg_enable = NETIF_MSG_LINK;
  439. db->mii.phy_id_mask = 0x1f;
  440. db->mii.reg_num_mask = 0x1f;
  441. db->mii.force_media = 0;
  442. db->mii.full_duplex = 0;
  443. db->mii.dev = ndev;
  444. db->mii.mdio_read = dm9000_phy_read;
  445. db->mii.mdio_write = dm9000_phy_write;
  446. /* Read SROM content */
  447. for (i = 0; i < 64; i++)
  448. ((u16 *) db->srom)[i] = read_srom_word(db, i);
  449. /* Set Node Address */
  450. for (i = 0; i < 6; i++)
  451. ndev->dev_addr[i] = db->srom[i];
  452. if (!is_valid_ether_addr(ndev->dev_addr)) {
  453. /* try reading from mac */
  454. for (i = 0; i < 6; i++)
  455. ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
  456. }
  457. if (!is_valid_ether_addr(ndev->dev_addr))
  458. printk("%s: Invalid ethernet MAC address. Please "
  459. "set using ifconfig\n", ndev->name);
  460. platform_set_drvdata(pdev, ndev);
  461. ret = register_netdev(ndev);
  462. if (ret == 0) {
  463. printk("%s: dm9000 at %p,%p IRQ %d MAC: ",
  464. ndev->name, db->io_addr, db->io_data, ndev->irq);
  465. for (i = 0; i < 5; i++)
  466. printk("%02x:", ndev->dev_addr[i]);
  467. printk("%02x\n", ndev->dev_addr[5]);
  468. }
  469. return 0;
  470. release:
  471. out:
  472. printk("%s: not found (%d).\n", CARDNAME, ret);
  473. dm9000_release_board(pdev, db);
  474. kfree(ndev);
  475. return ret;
  476. }
  477. /*
  478. * Open the interface.
  479. * The interface is opened whenever "ifconfig" actives it.
  480. */
  481. static int
  482. dm9000_open(struct net_device *dev)
  483. {
  484. board_info_t *db = (board_info_t *) dev->priv;
  485. PRINTK2("entering dm9000_open\n");
  486. if (request_irq(dev->irq, &dm9000_interrupt, IRQF_SHARED, dev->name, dev))
  487. return -EAGAIN;
  488. /* Initialize DM9000 board */
  489. dm9000_reset(db);
  490. dm9000_init_dm9000(dev);
  491. /* Init driver variable */
  492. db->dbug_cnt = 0;
  493. /* set and active a timer process */
  494. init_timer(&db->timer);
  495. db->timer.expires = DM9000_TIMER_WUT;
  496. db->timer.data = (unsigned long) dev;
  497. db->timer.function = &dm9000_timer;
  498. add_timer(&db->timer);
  499. mii_check_media(&db->mii, netif_msg_link(db), 1);
  500. netif_start_queue(dev);
  501. return 0;
  502. }
  503. /*
  504. * Initilize dm9000 board
  505. */
  506. static void
  507. dm9000_init_dm9000(struct net_device *dev)
  508. {
  509. board_info_t *db = (board_info_t *) dev->priv;
  510. PRINTK1("entering %s\n",__FUNCTION__);
  511. /* I/O mode */
  512. db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
  513. /* GPIO0 on pre-activate PHY */
  514. iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
  515. iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
  516. iow(db, DM9000_GPR, 0); /* Enable PHY */
  517. /* Program operating register */
  518. iow(db, DM9000_TCR, 0); /* TX Polling clear */
  519. iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  520. iow(db, DM9000_FCR, 0xff); /* Flow Control */
  521. iow(db, DM9000_SMCR, 0); /* Special Mode */
  522. /* clear TX status */
  523. iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  524. iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
  525. /* Set address filter table */
  526. dm9000_hash_table(dev);
  527. /* Activate DM9000 */
  528. iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
  529. /* Enable TX/RX interrupt mask */
  530. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  531. /* Init Driver variable */
  532. db->tx_pkt_cnt = 0;
  533. db->queue_pkt_len = 0;
  534. dev->trans_start = 0;
  535. }
  536. /*
  537. * Hardware start transmission.
  538. * Send a packet to media from the upper layer.
  539. */
  540. static int
  541. dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  542. {
  543. board_info_t *db = (board_info_t *) dev->priv;
  544. PRINTK3("dm9000_start_xmit\n");
  545. if (db->tx_pkt_cnt > 1)
  546. return 1;
  547. netif_stop_queue(dev);
  548. /* Disable all interrupts */
  549. iow(db, DM9000_IMR, IMR_PAR);
  550. /* Move data to DM9000 TX RAM */
  551. writeb(DM9000_MWCMD, db->io_addr);
  552. (db->outblk)(db->io_data, skb->data, skb->len);
  553. db->stats.tx_bytes += skb->len;
  554. /* TX control: First packet immediately send, second packet queue */
  555. if (db->tx_pkt_cnt == 0) {
  556. /* First Packet */
  557. db->tx_pkt_cnt++;
  558. /* Set TX length to DM9000 */
  559. iow(db, DM9000_TXPLL, skb->len & 0xff);
  560. iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
  561. /* Issue TX polling command */
  562. iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  563. dev->trans_start = jiffies; /* save the time stamp */
  564. } else {
  565. /* Second packet */
  566. db->tx_pkt_cnt++;
  567. db->queue_pkt_len = skb->len;
  568. }
  569. /* free this SKB */
  570. dev_kfree_skb(skb);
  571. /* Re-enable resource check */
  572. if (db->tx_pkt_cnt == 1)
  573. netif_wake_queue(dev);
  574. /* Re-enable interrupt */
  575. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  576. return 0;
  577. }
  578. static void
  579. dm9000_shutdown(struct net_device *dev)
  580. {
  581. board_info_t *db = (board_info_t *) dev->priv;
  582. /* RESET device */
  583. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  584. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  585. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  586. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  587. }
  588. /*
  589. * Stop the interface.
  590. * The interface is stopped when it is brought.
  591. */
  592. static int
  593. dm9000_stop(struct net_device *ndev)
  594. {
  595. board_info_t *db = (board_info_t *) ndev->priv;
  596. PRINTK1("entering %s\n",__FUNCTION__);
  597. /* deleted timer */
  598. del_timer(&db->timer);
  599. netif_stop_queue(ndev);
  600. netif_carrier_off(ndev);
  601. /* free interrupt */
  602. free_irq(ndev->irq, ndev);
  603. dm9000_shutdown(ndev);
  604. return 0;
  605. }
  606. /*
  607. * DM9000 interrupt handler
  608. * receive the packet to upper layer, free the transmitted packet
  609. */
  610. static void
  611. dm9000_tx_done(struct net_device *dev, board_info_t * db)
  612. {
  613. int tx_status = ior(db, DM9000_NSR); /* Got TX status */
  614. if (tx_status & (NSR_TX2END | NSR_TX1END)) {
  615. /* One packet sent complete */
  616. db->tx_pkt_cnt--;
  617. db->stats.tx_packets++;
  618. /* Queue packet check & send */
  619. if (db->tx_pkt_cnt > 0) {
  620. iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
  621. iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
  622. iow(db, DM9000_TCR, TCR_TXREQ);
  623. dev->trans_start = jiffies;
  624. }
  625. netif_wake_queue(dev);
  626. }
  627. }
  628. static irqreturn_t
  629. dm9000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  630. {
  631. struct net_device *dev = dev_id;
  632. board_info_t *db;
  633. int int_status;
  634. u8 reg_save;
  635. PRINTK3("entering %s\n",__FUNCTION__);
  636. if (!dev) {
  637. PRINTK1("dm9000_interrupt() without DEVICE arg\n");
  638. return IRQ_HANDLED;
  639. }
  640. /* A real interrupt coming */
  641. db = (board_info_t *) dev->priv;
  642. spin_lock(&db->lock);
  643. /* Save previous register address */
  644. reg_save = readb(db->io_addr);
  645. /* Disable all interrupts */
  646. iow(db, DM9000_IMR, IMR_PAR);
  647. /* Got DM9000 interrupt status */
  648. int_status = ior(db, DM9000_ISR); /* Got ISR */
  649. iow(db, DM9000_ISR, int_status); /* Clear ISR status */
  650. /* Received the coming packet */
  651. if (int_status & ISR_PRS)
  652. dm9000_rx(dev);
  653. /* Trnasmit Interrupt check */
  654. if (int_status & ISR_PTS)
  655. dm9000_tx_done(dev, db);
  656. /* Re-enable interrupt mask */
  657. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  658. /* Restore previous register address */
  659. writeb(reg_save, db->io_addr);
  660. spin_unlock(&db->lock);
  661. return IRQ_HANDLED;
  662. }
  663. /*
  664. * Get statistics from driver.
  665. */
  666. static struct net_device_stats *
  667. dm9000_get_stats(struct net_device *dev)
  668. {
  669. board_info_t *db = (board_info_t *) dev->priv;
  670. return &db->stats;
  671. }
  672. /*
  673. * A periodic timer routine
  674. * Dynamic media sense, allocated Rx buffer...
  675. */
  676. static void
  677. dm9000_timer(unsigned long data)
  678. {
  679. struct net_device *dev = (struct net_device *) data;
  680. board_info_t *db = (board_info_t *) dev->priv;
  681. PRINTK3("dm9000_timer()\n");
  682. mii_check_media(&db->mii, netif_msg_link(db), 0);
  683. /* Set timer again */
  684. db->timer.expires = DM9000_TIMER_WUT;
  685. add_timer(&db->timer);
  686. }
  687. struct dm9000_rxhdr {
  688. u16 RxStatus;
  689. u16 RxLen;
  690. } __attribute__((__packed__));
  691. /*
  692. * Received a packet and pass to upper layer
  693. */
  694. static void
  695. dm9000_rx(struct net_device *dev)
  696. {
  697. board_info_t *db = (board_info_t *) dev->priv;
  698. struct dm9000_rxhdr rxhdr;
  699. struct sk_buff *skb;
  700. u8 rxbyte, *rdptr;
  701. int GoodPacket;
  702. int RxLen;
  703. /* Check packet ready or not */
  704. do {
  705. ior(db, DM9000_MRCMDX); /* Dummy read */
  706. /* Get most updated data */
  707. rxbyte = readb(db->io_data);
  708. /* Status check: this byte must be 0 or 1 */
  709. if (rxbyte > DM9000_PKT_RDY) {
  710. printk("status check failed: %d\n", rxbyte);
  711. iow(db, DM9000_RCR, 0x00); /* Stop Device */
  712. iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
  713. return;
  714. }
  715. if (rxbyte != DM9000_PKT_RDY)
  716. return;
  717. /* A packet ready now & Get status/length */
  718. GoodPacket = TRUE;
  719. writeb(DM9000_MRCMD, db->io_addr);
  720. (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
  721. RxLen = rxhdr.RxLen;
  722. /* Packet Status check */
  723. if (RxLen < 0x40) {
  724. GoodPacket = FALSE;
  725. PRINTK1("Bad Packet received (runt)\n");
  726. }
  727. if (RxLen > DM9000_PKT_MAX) {
  728. PRINTK1("RST: RX Len:%x\n", RxLen);
  729. }
  730. if (rxhdr.RxStatus & 0xbf00) {
  731. GoodPacket = FALSE;
  732. if (rxhdr.RxStatus & 0x100) {
  733. PRINTK1("fifo error\n");
  734. db->stats.rx_fifo_errors++;
  735. }
  736. if (rxhdr.RxStatus & 0x200) {
  737. PRINTK1("crc error\n");
  738. db->stats.rx_crc_errors++;
  739. }
  740. if (rxhdr.RxStatus & 0x8000) {
  741. PRINTK1("length error\n");
  742. db->stats.rx_length_errors++;
  743. }
  744. }
  745. /* Move data from DM9000 */
  746. if (GoodPacket
  747. && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
  748. skb->dev = dev;
  749. skb_reserve(skb, 2);
  750. rdptr = (u8 *) skb_put(skb, RxLen - 4);
  751. /* Read received packet from RX SRAM */
  752. (db->inblk)(db->io_data, rdptr, RxLen);
  753. db->stats.rx_bytes += RxLen;
  754. /* Pass to upper layer */
  755. skb->protocol = eth_type_trans(skb, dev);
  756. netif_rx(skb);
  757. db->stats.rx_packets++;
  758. } else {
  759. /* need to dump the packet's data */
  760. (db->dumpblk)(db->io_data, RxLen);
  761. }
  762. } while (rxbyte == DM9000_PKT_RDY);
  763. }
  764. /*
  765. * Read a word data from SROM
  766. */
  767. static u16
  768. read_srom_word(board_info_t * db, int offset)
  769. {
  770. iow(db, DM9000_EPAR, offset);
  771. iow(db, DM9000_EPCR, EPCR_ERPRR);
  772. mdelay(8); /* according to the datasheet 200us should be enough,
  773. but it doesn't work */
  774. iow(db, DM9000_EPCR, 0x0);
  775. return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
  776. }
  777. #ifdef DM9000_PROGRAM_EEPROM
  778. /*
  779. * Write a word data to SROM
  780. */
  781. static void
  782. write_srom_word(board_info_t * db, int offset, u16 val)
  783. {
  784. iow(db, DM9000_EPAR, offset);
  785. iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
  786. iow(db, DM9000_EPDRL, (val & 0xff));
  787. iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
  788. mdelay(8); /* same shit */
  789. iow(db, DM9000_EPCR, 0);
  790. }
  791. /*
  792. * Only for development:
  793. * Here we write static data to the eeprom in case
  794. * we don't have valid content on a new board
  795. */
  796. static void
  797. program_eeprom(board_info_t * db)
  798. {
  799. u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
  800. 0x0000, /* Autoload: accept nothing */
  801. 0x0a46, 0x9000, /* Vendor / Product ID */
  802. 0x0000, /* pin control */
  803. 0x0000,
  804. }; /* Wake-up mode control */
  805. int i;
  806. for (i = 0; i < 8; i++)
  807. write_srom_word(db, i, eeprom[i]);
  808. }
  809. #endif
  810. /*
  811. * Calculate the CRC valude of the Rx packet
  812. * flag = 1 : return the reverse CRC (for the received packet CRC)
  813. * 0 : return the normal CRC (for Hash Table index)
  814. */
  815. static unsigned long
  816. cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
  817. {
  818. u32 crc = ether_crc_le(Len, Data);
  819. if (flag)
  820. return ~crc;
  821. return crc;
  822. }
  823. /*
  824. * Set DM9000 multicast address
  825. */
  826. static void
  827. dm9000_hash_table(struct net_device *dev)
  828. {
  829. board_info_t *db = (board_info_t *) dev->priv;
  830. struct dev_mc_list *mcptr = dev->mc_list;
  831. int mc_cnt = dev->mc_count;
  832. u32 hash_val;
  833. u16 i, oft, hash_table[4];
  834. unsigned long flags;
  835. PRINTK2("dm9000_hash_table()\n");
  836. spin_lock_irqsave(&db->lock,flags);
  837. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  838. iow(db, oft, dev->dev_addr[i]);
  839. /* Clear Hash Table */
  840. for (i = 0; i < 4; i++)
  841. hash_table[i] = 0x0;
  842. /* broadcast address */
  843. hash_table[3] = 0x8000;
  844. /* the multicast address in Hash Table : 64 bits */
  845. for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
  846. hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
  847. hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
  848. }
  849. /* Write the hash table to MAC MD table */
  850. for (i = 0, oft = 0x16; i < 4; i++) {
  851. iow(db, oft++, hash_table[i] & 0xff);
  852. iow(db, oft++, (hash_table[i] >> 8) & 0xff);
  853. }
  854. spin_unlock_irqrestore(&db->lock,flags);
  855. }
  856. /*
  857. * Read a word from phyxcer
  858. */
  859. static int
  860. dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
  861. {
  862. board_info_t *db = (board_info_t *) dev->priv;
  863. unsigned long flags;
  864. unsigned int reg_save;
  865. int ret;
  866. spin_lock_irqsave(&db->lock,flags);
  867. /* Save previous register address */
  868. reg_save = readb(db->io_addr);
  869. /* Fill the phyxcer register into REG_0C */
  870. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  871. iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
  872. udelay(100); /* Wait read complete */
  873. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  874. /* The read data keeps on REG_0D & REG_0E */
  875. ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
  876. /* restore the previous address */
  877. writeb(reg_save, db->io_addr);
  878. spin_unlock_irqrestore(&db->lock,flags);
  879. return ret;
  880. }
  881. /*
  882. * Write a word to phyxcer
  883. */
  884. static void
  885. dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
  886. {
  887. board_info_t *db = (board_info_t *) dev->priv;
  888. unsigned long flags;
  889. unsigned long reg_save;
  890. spin_lock_irqsave(&db->lock,flags);
  891. /* Save previous register address */
  892. reg_save = readb(db->io_addr);
  893. /* Fill the phyxcer register into REG_0C */
  894. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  895. /* Fill the written data into REG_0D & REG_0E */
  896. iow(db, DM9000_EPDRL, (value & 0xff));
  897. iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
  898. iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
  899. udelay(500); /* Wait write complete */
  900. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  901. /* restore the previous address */
  902. writeb(reg_save, db->io_addr);
  903. spin_unlock_irqrestore(&db->lock,flags);
  904. }
  905. static int
  906. dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
  907. {
  908. struct net_device *ndev = platform_get_drvdata(dev);
  909. if (ndev) {
  910. if (netif_running(ndev)) {
  911. netif_device_detach(ndev);
  912. dm9000_shutdown(ndev);
  913. }
  914. }
  915. return 0;
  916. }
  917. static int
  918. dm9000_drv_resume(struct platform_device *dev)
  919. {
  920. struct net_device *ndev = platform_get_drvdata(dev);
  921. board_info_t *db = (board_info_t *) ndev->priv;
  922. if (ndev) {
  923. if (netif_running(ndev)) {
  924. dm9000_reset(db);
  925. dm9000_init_dm9000(ndev);
  926. netif_device_attach(ndev);
  927. }
  928. }
  929. return 0;
  930. }
  931. static int
  932. dm9000_drv_remove(struct platform_device *pdev)
  933. {
  934. struct net_device *ndev = platform_get_drvdata(pdev);
  935. platform_set_drvdata(pdev, NULL);
  936. unregister_netdev(ndev);
  937. dm9000_release_board(pdev, (board_info_t *) ndev->priv);
  938. kfree(ndev); /* free device structure */
  939. PRINTK1("clean_module() exit\n");
  940. return 0;
  941. }
  942. static struct platform_driver dm9000_driver = {
  943. .driver = {
  944. .name = "dm9000",
  945. .owner = THIS_MODULE,
  946. },
  947. .probe = dm9000_probe,
  948. .remove = dm9000_drv_remove,
  949. .suspend = dm9000_drv_suspend,
  950. .resume = dm9000_drv_resume,
  951. };
  952. static int __init
  953. dm9000_init(void)
  954. {
  955. printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
  956. return platform_driver_register(&dm9000_driver); /* search board and register */
  957. }
  958. static void __exit
  959. dm9000_cleanup(void)
  960. {
  961. platform_driver_unregister(&dm9000_driver);
  962. }
  963. module_init(dm9000_init);
  964. module_exit(dm9000_cleanup);
  965. MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
  966. MODULE_DESCRIPTION("Davicom DM9000 network driver");
  967. MODULE_LICENSE("GPL");