dm9000.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * Davicom DM9000 Fast Ethernet driver for Linux.
  3. * Copyright (C) 1997 Sten Wang
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
  16. *
  17. * Additional updates, Copyright:
  18. * Ben Dooks <ben@simtec.co.uk>
  19. * Sascha Hauer <s.hauer@pengutronix.de>
  20. */
  21. #include <linux/module.h>
  22. #include <linux/ioport.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/crc32.h>
  30. #include <linux/mii.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/dm9000.h>
  33. #include <linux/delay.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/irq.h>
  36. #include <linux/slab.h>
  37. #include <asm/delay.h>
  38. #include <asm/irq.h>
  39. #include <asm/io.h>
  40. #include "dm9000.h"
  41. /* Board/System/Debug information/definition ---------------- */
  42. #define DM9000_PHY 0x40 /* PHY address 0x01 */
  43. #define CARDNAME "dm9000"
  44. #define DRV_VERSION "1.31"
  45. /*
  46. * Transmit timeout, default 5 seconds.
  47. */
  48. static int watchdog = 5000;
  49. module_param(watchdog, int, 0400);
  50. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
  51. /* DM9000 register address locking.
  52. *
  53. * The DM9000 uses an address register to control where data written
  54. * to the data register goes. This means that the address register
  55. * must be preserved over interrupts or similar calls.
  56. *
  57. * During interrupt and other critical calls, a spinlock is used to
  58. * protect the system, but the calls themselves save the address
  59. * in the address register in case they are interrupting another
  60. * access to the device.
  61. *
  62. * For general accesses a lock is provided so that calls which are
  63. * allowed to sleep are serialised so that the address register does
  64. * not need to be saved. This lock also serves to serialise access
  65. * to the EEPROM and PHY access registers which are shared between
  66. * these two devices.
  67. */
  68. /* The driver supports the original DM9000E, and now the two newer
  69. * devices, DM9000A and DM9000B.
  70. */
  71. enum dm9000_type {
  72. TYPE_DM9000E, /* original DM9000 */
  73. TYPE_DM9000A,
  74. TYPE_DM9000B
  75. };
  76. /* Structure/enum declaration ------------------------------- */
  77. typedef struct board_info {
  78. void __iomem *io_addr; /* Register I/O base address */
  79. void __iomem *io_data; /* Data I/O address */
  80. u16 irq; /* IRQ */
  81. u16 tx_pkt_cnt;
  82. u16 queue_pkt_len;
  83. u16 queue_start_addr;
  84. u16 queue_ip_summed;
  85. u16 dbug_cnt;
  86. u8 io_mode; /* 0:word, 2:byte */
  87. u8 phy_addr;
  88. u8 imr_all;
  89. unsigned int flags;
  90. unsigned int in_suspend :1;
  91. unsigned int wake_supported :1;
  92. int debug_level;
  93. enum dm9000_type type;
  94. void (*inblk)(void __iomem *port, void *data, int length);
  95. void (*outblk)(void __iomem *port, void *data, int length);
  96. void (*dumpblk)(void __iomem *port, int length);
  97. struct device *dev; /* parent device */
  98. struct resource *addr_res; /* resources found */
  99. struct resource *data_res;
  100. struct resource *addr_req; /* resources requested */
  101. struct resource *data_req;
  102. struct resource *irq_res;
  103. int irq_wake;
  104. struct mutex addr_lock; /* phy and eeprom access lock */
  105. struct delayed_work phy_poll;
  106. struct net_device *ndev;
  107. spinlock_t lock;
  108. struct mii_if_info mii;
  109. u32 msg_enable;
  110. u32 wake_state;
  111. int ip_summed;
  112. } board_info_t;
  113. /* debug code */
  114. #define dm9000_dbg(db, lev, msg...) do { \
  115. if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
  116. (lev) < db->debug_level) { \
  117. dev_dbg(db->dev, msg); \
  118. } \
  119. } while (0)
  120. static inline board_info_t *to_dm9000_board(struct net_device *dev)
  121. {
  122. return netdev_priv(dev);
  123. }
  124. /* DM9000 network board routine ---------------------------- */
  125. static void
  126. dm9000_reset(board_info_t * db)
  127. {
  128. dev_dbg(db->dev, "resetting device\n");
  129. /* RESET device */
  130. writeb(DM9000_NCR, db->io_addr);
  131. udelay(200);
  132. writeb(NCR_RST, db->io_data);
  133. udelay(200);
  134. }
  135. /*
  136. * Read a byte from I/O port
  137. */
  138. static u8
  139. ior(board_info_t * db, int reg)
  140. {
  141. writeb(reg, db->io_addr);
  142. return readb(db->io_data);
  143. }
  144. /*
  145. * Write a byte to I/O port
  146. */
  147. static void
  148. iow(board_info_t * db, int reg, int value)
  149. {
  150. writeb(reg, db->io_addr);
  151. writeb(value, db->io_data);
  152. }
  153. /* routines for sending block to chip */
  154. static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
  155. {
  156. writesb(reg, data, count);
  157. }
  158. static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
  159. {
  160. writesw(reg, data, (count+1) >> 1);
  161. }
  162. static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
  163. {
  164. writesl(reg, data, (count+3) >> 2);
  165. }
  166. /* input block from chip to memory */
  167. static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
  168. {
  169. readsb(reg, data, count);
  170. }
  171. static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
  172. {
  173. readsw(reg, data, (count+1) >> 1);
  174. }
  175. static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
  176. {
  177. readsl(reg, data, (count+3) >> 2);
  178. }
  179. /* dump block from chip to null */
  180. static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
  181. {
  182. int i;
  183. int tmp;
  184. for (i = 0; i < count; i++)
  185. tmp = readb(reg);
  186. }
  187. static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
  188. {
  189. int i;
  190. int tmp;
  191. count = (count + 1) >> 1;
  192. for (i = 0; i < count; i++)
  193. tmp = readw(reg);
  194. }
  195. static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
  196. {
  197. int i;
  198. int tmp;
  199. count = (count + 3) >> 2;
  200. for (i = 0; i < count; i++)
  201. tmp = readl(reg);
  202. }
  203. /* dm9000_set_io
  204. *
  205. * select the specified set of io routines to use with the
  206. * device
  207. */
  208. static void dm9000_set_io(struct board_info *db, int byte_width)
  209. {
  210. /* use the size of the data resource to work out what IO
  211. * routines we want to use
  212. */
  213. switch (byte_width) {
  214. case 1:
  215. db->dumpblk = dm9000_dumpblk_8bit;
  216. db->outblk = dm9000_outblk_8bit;
  217. db->inblk = dm9000_inblk_8bit;
  218. break;
  219. case 3:
  220. dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
  221. case 2:
  222. db->dumpblk = dm9000_dumpblk_16bit;
  223. db->outblk = dm9000_outblk_16bit;
  224. db->inblk = dm9000_inblk_16bit;
  225. break;
  226. case 4:
  227. default:
  228. db->dumpblk = dm9000_dumpblk_32bit;
  229. db->outblk = dm9000_outblk_32bit;
  230. db->inblk = dm9000_inblk_32bit;
  231. break;
  232. }
  233. }
  234. static void dm9000_schedule_poll(board_info_t *db)
  235. {
  236. if (db->type == TYPE_DM9000E)
  237. schedule_delayed_work(&db->phy_poll, HZ * 2);
  238. }
  239. static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  240. {
  241. board_info_t *dm = to_dm9000_board(dev);
  242. if (!netif_running(dev))
  243. return -EINVAL;
  244. return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
  245. }
  246. static unsigned int
  247. dm9000_read_locked(board_info_t *db, int reg)
  248. {
  249. unsigned long flags;
  250. unsigned int ret;
  251. spin_lock_irqsave(&db->lock, flags);
  252. ret = ior(db, reg);
  253. spin_unlock_irqrestore(&db->lock, flags);
  254. return ret;
  255. }
  256. static int dm9000_wait_eeprom(board_info_t *db)
  257. {
  258. unsigned int status;
  259. int timeout = 8; /* wait max 8msec */
  260. /* The DM9000 data sheets say we should be able to
  261. * poll the ERRE bit in EPCR to wait for the EEPROM
  262. * operation. From testing several chips, this bit
  263. * does not seem to work.
  264. *
  265. * We attempt to use the bit, but fall back to the
  266. * timeout (which is why we do not return an error
  267. * on expiry) to say that the EEPROM operation has
  268. * completed.
  269. */
  270. while (1) {
  271. status = dm9000_read_locked(db, DM9000_EPCR);
  272. if ((status & EPCR_ERRE) == 0)
  273. break;
  274. msleep(1);
  275. if (timeout-- < 0) {
  276. dev_dbg(db->dev, "timeout waiting EEPROM\n");
  277. break;
  278. }
  279. }
  280. return 0;
  281. }
  282. /*
  283. * Read a word data from EEPROM
  284. */
  285. static void
  286. dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
  287. {
  288. unsigned long flags;
  289. if (db->flags & DM9000_PLATF_NO_EEPROM) {
  290. to[0] = 0xff;
  291. to[1] = 0xff;
  292. return;
  293. }
  294. mutex_lock(&db->addr_lock);
  295. spin_lock_irqsave(&db->lock, flags);
  296. iow(db, DM9000_EPAR, offset);
  297. iow(db, DM9000_EPCR, EPCR_ERPRR);
  298. spin_unlock_irqrestore(&db->lock, flags);
  299. dm9000_wait_eeprom(db);
  300. /* delay for at-least 150uS */
  301. msleep(1);
  302. spin_lock_irqsave(&db->lock, flags);
  303. iow(db, DM9000_EPCR, 0x0);
  304. to[0] = ior(db, DM9000_EPDRL);
  305. to[1] = ior(db, DM9000_EPDRH);
  306. spin_unlock_irqrestore(&db->lock, flags);
  307. mutex_unlock(&db->addr_lock);
  308. }
  309. /*
  310. * Write a word data to SROM
  311. */
  312. static void
  313. dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
  314. {
  315. unsigned long flags;
  316. if (db->flags & DM9000_PLATF_NO_EEPROM)
  317. return;
  318. mutex_lock(&db->addr_lock);
  319. spin_lock_irqsave(&db->lock, flags);
  320. iow(db, DM9000_EPAR, offset);
  321. iow(db, DM9000_EPDRH, data[1]);
  322. iow(db, DM9000_EPDRL, data[0]);
  323. iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
  324. spin_unlock_irqrestore(&db->lock, flags);
  325. dm9000_wait_eeprom(db);
  326. mdelay(1); /* wait at least 150uS to clear */
  327. spin_lock_irqsave(&db->lock, flags);
  328. iow(db, DM9000_EPCR, 0);
  329. spin_unlock_irqrestore(&db->lock, flags);
  330. mutex_unlock(&db->addr_lock);
  331. }
  332. /* ethtool ops */
  333. static void dm9000_get_drvinfo(struct net_device *dev,
  334. struct ethtool_drvinfo *info)
  335. {
  336. board_info_t *dm = to_dm9000_board(dev);
  337. strcpy(info->driver, CARDNAME);
  338. strcpy(info->version, DRV_VERSION);
  339. strcpy(info->bus_info, to_platform_device(dm->dev)->name);
  340. }
  341. static u32 dm9000_get_msglevel(struct net_device *dev)
  342. {
  343. board_info_t *dm = to_dm9000_board(dev);
  344. return dm->msg_enable;
  345. }
  346. static void dm9000_set_msglevel(struct net_device *dev, u32 value)
  347. {
  348. board_info_t *dm = to_dm9000_board(dev);
  349. dm->msg_enable = value;
  350. }
  351. static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  352. {
  353. board_info_t *dm = to_dm9000_board(dev);
  354. mii_ethtool_gset(&dm->mii, cmd);
  355. return 0;
  356. }
  357. static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  358. {
  359. board_info_t *dm = to_dm9000_board(dev);
  360. return mii_ethtool_sset(&dm->mii, cmd);
  361. }
  362. static int dm9000_nway_reset(struct net_device *dev)
  363. {
  364. board_info_t *dm = to_dm9000_board(dev);
  365. return mii_nway_restart(&dm->mii);
  366. }
  367. static int dm9000_set_features(struct net_device *dev, u32 features)
  368. {
  369. board_info_t *dm = to_dm9000_board(dev);
  370. u32 changed = dev->features ^ features;
  371. unsigned long flags;
  372. if (!(changed & NETIF_F_RXCSUM))
  373. return 0;
  374. spin_lock_irqsave(&dm->lock, flags);
  375. iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
  376. spin_unlock_irqrestore(&dm->lock, flags);
  377. return 0;
  378. }
  379. static u32 dm9000_get_link(struct net_device *dev)
  380. {
  381. board_info_t *dm = to_dm9000_board(dev);
  382. u32 ret;
  383. if (dm->flags & DM9000_PLATF_EXT_PHY)
  384. ret = mii_link_ok(&dm->mii);
  385. else
  386. ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
  387. return ret;
  388. }
  389. #define DM_EEPROM_MAGIC (0x444D394B)
  390. static int dm9000_get_eeprom_len(struct net_device *dev)
  391. {
  392. return 128;
  393. }
  394. static int dm9000_get_eeprom(struct net_device *dev,
  395. struct ethtool_eeprom *ee, u8 *data)
  396. {
  397. board_info_t *dm = to_dm9000_board(dev);
  398. int offset = ee->offset;
  399. int len = ee->len;
  400. int i;
  401. /* EEPROM access is aligned to two bytes */
  402. if ((len & 1) != 0 || (offset & 1) != 0)
  403. return -EINVAL;
  404. if (dm->flags & DM9000_PLATF_NO_EEPROM)
  405. return -ENOENT;
  406. ee->magic = DM_EEPROM_MAGIC;
  407. for (i = 0; i < len; i += 2)
  408. dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
  409. return 0;
  410. }
  411. static int dm9000_set_eeprom(struct net_device *dev,
  412. struct ethtool_eeprom *ee, u8 *data)
  413. {
  414. board_info_t *dm = to_dm9000_board(dev);
  415. int offset = ee->offset;
  416. int len = ee->len;
  417. int done;
  418. /* EEPROM access is aligned to two bytes */
  419. if (dm->flags & DM9000_PLATF_NO_EEPROM)
  420. return -ENOENT;
  421. if (ee->magic != DM_EEPROM_MAGIC)
  422. return -EINVAL;
  423. while (len > 0) {
  424. if (len & 1 || offset & 1) {
  425. int which = offset & 1;
  426. u8 tmp[2];
  427. dm9000_read_eeprom(dm, offset / 2, tmp);
  428. tmp[which] = *data;
  429. dm9000_write_eeprom(dm, offset / 2, tmp);
  430. done = 1;
  431. } else {
  432. dm9000_write_eeprom(dm, offset / 2, data);
  433. done = 2;
  434. }
  435. data += done;
  436. offset += done;
  437. len -= done;
  438. }
  439. return 0;
  440. }
  441. static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  442. {
  443. board_info_t *dm = to_dm9000_board(dev);
  444. memset(w, 0, sizeof(struct ethtool_wolinfo));
  445. /* note, we could probably support wake-phy too */
  446. w->supported = dm->wake_supported ? WAKE_MAGIC : 0;
  447. w->wolopts = dm->wake_state;
  448. }
  449. static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  450. {
  451. board_info_t *dm = to_dm9000_board(dev);
  452. unsigned long flags;
  453. u32 opts = w->wolopts;
  454. u32 wcr = 0;
  455. if (!dm->wake_supported)
  456. return -EOPNOTSUPP;
  457. if (opts & ~WAKE_MAGIC)
  458. return -EINVAL;
  459. if (opts & WAKE_MAGIC)
  460. wcr |= WCR_MAGICEN;
  461. mutex_lock(&dm->addr_lock);
  462. spin_lock_irqsave(&dm->lock, flags);
  463. iow(dm, DM9000_WCR, wcr);
  464. spin_unlock_irqrestore(&dm->lock, flags);
  465. mutex_unlock(&dm->addr_lock);
  466. if (dm->wake_state != opts) {
  467. /* change in wol state, update IRQ state */
  468. if (!dm->wake_state)
  469. irq_set_irq_wake(dm->irq_wake, 1);
  470. else if (dm->wake_state & !opts)
  471. irq_set_irq_wake(dm->irq_wake, 0);
  472. }
  473. dm->wake_state = opts;
  474. return 0;
  475. }
  476. static const struct ethtool_ops dm9000_ethtool_ops = {
  477. .get_drvinfo = dm9000_get_drvinfo,
  478. .get_settings = dm9000_get_settings,
  479. .set_settings = dm9000_set_settings,
  480. .get_msglevel = dm9000_get_msglevel,
  481. .set_msglevel = dm9000_set_msglevel,
  482. .nway_reset = dm9000_nway_reset,
  483. .get_link = dm9000_get_link,
  484. .get_wol = dm9000_get_wol,
  485. .set_wol = dm9000_set_wol,
  486. .get_eeprom_len = dm9000_get_eeprom_len,
  487. .get_eeprom = dm9000_get_eeprom,
  488. .set_eeprom = dm9000_set_eeprom,
  489. };
  490. static void dm9000_show_carrier(board_info_t *db,
  491. unsigned carrier, unsigned nsr)
  492. {
  493. struct net_device *ndev = db->ndev;
  494. unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
  495. if (carrier)
  496. dev_info(db->dev, "%s: link up, %dMbps, %s-duplex, no LPA\n",
  497. ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
  498. (ncr & NCR_FDX) ? "full" : "half");
  499. else
  500. dev_info(db->dev, "%s: link down\n", ndev->name);
  501. }
  502. static void
  503. dm9000_poll_work(struct work_struct *w)
  504. {
  505. struct delayed_work *dw = to_delayed_work(w);
  506. board_info_t *db = container_of(dw, board_info_t, phy_poll);
  507. struct net_device *ndev = db->ndev;
  508. if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
  509. !(db->flags & DM9000_PLATF_EXT_PHY)) {
  510. unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
  511. unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
  512. unsigned new_carrier;
  513. new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
  514. if (old_carrier != new_carrier) {
  515. if (netif_msg_link(db))
  516. dm9000_show_carrier(db, new_carrier, nsr);
  517. if (!new_carrier)
  518. netif_carrier_off(ndev);
  519. else
  520. netif_carrier_on(ndev);
  521. }
  522. } else
  523. mii_check_media(&db->mii, netif_msg_link(db), 0);
  524. if (netif_running(ndev))
  525. dm9000_schedule_poll(db);
  526. }
  527. /* dm9000_release_board
  528. *
  529. * release a board, and any mapped resources
  530. */
  531. static void
  532. dm9000_release_board(struct platform_device *pdev, struct board_info *db)
  533. {
  534. /* unmap our resources */
  535. iounmap(db->io_addr);
  536. iounmap(db->io_data);
  537. /* release the resources */
  538. release_resource(db->data_req);
  539. kfree(db->data_req);
  540. release_resource(db->addr_req);
  541. kfree(db->addr_req);
  542. }
  543. static unsigned char dm9000_type_to_char(enum dm9000_type type)
  544. {
  545. switch (type) {
  546. case TYPE_DM9000E: return 'e';
  547. case TYPE_DM9000A: return 'a';
  548. case TYPE_DM9000B: return 'b';
  549. }
  550. return '?';
  551. }
  552. /*
  553. * Set DM9000 multicast address
  554. */
  555. static void
  556. dm9000_hash_table_unlocked(struct net_device *dev)
  557. {
  558. board_info_t *db = netdev_priv(dev);
  559. struct netdev_hw_addr *ha;
  560. int i, oft;
  561. u32 hash_val;
  562. u16 hash_table[4];
  563. u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
  564. dm9000_dbg(db, 1, "entering %s\n", __func__);
  565. for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
  566. iow(db, oft, dev->dev_addr[i]);
  567. /* Clear Hash Table */
  568. for (i = 0; i < 4; i++)
  569. hash_table[i] = 0x0;
  570. /* broadcast address */
  571. hash_table[3] = 0x8000;
  572. if (dev->flags & IFF_PROMISC)
  573. rcr |= RCR_PRMSC;
  574. if (dev->flags & IFF_ALLMULTI)
  575. rcr |= RCR_ALL;
  576. /* the multicast address in Hash Table : 64 bits */
  577. netdev_for_each_mc_addr(ha, dev) {
  578. hash_val = ether_crc_le(6, ha->addr) & 0x3f;
  579. hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
  580. }
  581. /* Write the hash table to MAC MD table */
  582. for (i = 0, oft = DM9000_MAR; i < 4; i++) {
  583. iow(db, oft++, hash_table[i]);
  584. iow(db, oft++, hash_table[i] >> 8);
  585. }
  586. iow(db, DM9000_RCR, rcr);
  587. }
  588. static void
  589. dm9000_hash_table(struct net_device *dev)
  590. {
  591. board_info_t *db = netdev_priv(dev);
  592. unsigned long flags;
  593. spin_lock_irqsave(&db->lock, flags);
  594. dm9000_hash_table_unlocked(dev);
  595. spin_unlock_irqrestore(&db->lock, flags);
  596. }
  597. /*
  598. * Initialize dm9000 board
  599. */
  600. static void
  601. dm9000_init_dm9000(struct net_device *dev)
  602. {
  603. board_info_t *db = netdev_priv(dev);
  604. unsigned int imr;
  605. unsigned int ncr;
  606. dm9000_dbg(db, 1, "entering %s\n", __func__);
  607. /* I/O mode */
  608. db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
  609. /* Checksum mode */
  610. if (dev->hw_features & NETIF_F_RXCSUM)
  611. iow(db, DM9000_RCSR,
  612. (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
  613. iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
  614. ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0;
  615. /* if wol is needed, then always set NCR_WAKEEN otherwise we end
  616. * up dumping the wake events if we disable this. There is already
  617. * a wake-mask in DM9000_WCR */
  618. if (db->wake_supported)
  619. ncr |= NCR_WAKEEN;
  620. iow(db, DM9000_NCR, ncr);
  621. /* Program operating register */
  622. iow(db, DM9000_TCR, 0); /* TX Polling clear */
  623. iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  624. iow(db, DM9000_FCR, 0xff); /* Flow Control */
  625. iow(db, DM9000_SMCR, 0); /* Special Mode */
  626. /* clear TX status */
  627. iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  628. iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
  629. /* Set address filter table */
  630. dm9000_hash_table_unlocked(dev);
  631. imr = IMR_PAR | IMR_PTM | IMR_PRM;
  632. if (db->type != TYPE_DM9000E)
  633. imr |= IMR_LNKCHNG;
  634. db->imr_all = imr;
  635. /* Enable TX/RX interrupt mask */
  636. iow(db, DM9000_IMR, imr);
  637. /* Init Driver variable */
  638. db->tx_pkt_cnt = 0;
  639. db->queue_pkt_len = 0;
  640. dev->trans_start = jiffies;
  641. }
  642. /* Our watchdog timed out. Called by the networking layer */
  643. static void dm9000_timeout(struct net_device *dev)
  644. {
  645. board_info_t *db = netdev_priv(dev);
  646. u8 reg_save;
  647. unsigned long flags;
  648. /* Save previous register address */
  649. spin_lock_irqsave(&db->lock, flags);
  650. reg_save = readb(db->io_addr);
  651. netif_stop_queue(dev);
  652. dm9000_reset(db);
  653. dm9000_init_dm9000(dev);
  654. /* We can accept TX packets again */
  655. dev->trans_start = jiffies; /* prevent tx timeout */
  656. netif_wake_queue(dev);
  657. /* Restore previous register address */
  658. writeb(reg_save, db->io_addr);
  659. spin_unlock_irqrestore(&db->lock, flags);
  660. }
  661. static void dm9000_send_packet(struct net_device *dev,
  662. int ip_summed,
  663. u16 pkt_len)
  664. {
  665. board_info_t *dm = to_dm9000_board(dev);
  666. /* The DM9000 is not smart enough to leave fragmented packets alone. */
  667. if (dm->ip_summed != ip_summed) {
  668. if (ip_summed == CHECKSUM_NONE)
  669. iow(dm, DM9000_TCCR, 0);
  670. else
  671. iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
  672. dm->ip_summed = ip_summed;
  673. }
  674. /* Set TX length to DM9000 */
  675. iow(dm, DM9000_TXPLL, pkt_len);
  676. iow(dm, DM9000_TXPLH, pkt_len >> 8);
  677. /* Issue TX polling command */
  678. iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  679. }
  680. /*
  681. * Hardware start transmission.
  682. * Send a packet to media from the upper layer.
  683. */
  684. static int
  685. dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  686. {
  687. unsigned long flags;
  688. board_info_t *db = netdev_priv(dev);
  689. dm9000_dbg(db, 3, "%s:\n", __func__);
  690. if (db->tx_pkt_cnt > 1)
  691. return NETDEV_TX_BUSY;
  692. spin_lock_irqsave(&db->lock, flags);
  693. /* Move data to DM9000 TX RAM */
  694. writeb(DM9000_MWCMD, db->io_addr);
  695. (db->outblk)(db->io_data, skb->data, skb->len);
  696. dev->stats.tx_bytes += skb->len;
  697. db->tx_pkt_cnt++;
  698. /* TX control: First packet immediately send, second packet queue */
  699. if (db->tx_pkt_cnt == 1) {
  700. dm9000_send_packet(dev, skb->ip_summed, skb->len);
  701. } else {
  702. /* Second packet */
  703. db->queue_pkt_len = skb->len;
  704. db->queue_ip_summed = skb->ip_summed;
  705. netif_stop_queue(dev);
  706. }
  707. spin_unlock_irqrestore(&db->lock, flags);
  708. /* free this SKB */
  709. dev_kfree_skb(skb);
  710. return NETDEV_TX_OK;
  711. }
  712. /*
  713. * DM9000 interrupt handler
  714. * receive the packet to upper layer, free the transmitted packet
  715. */
  716. static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
  717. {
  718. int tx_status = ior(db, DM9000_NSR); /* Got TX status */
  719. if (tx_status & (NSR_TX2END | NSR_TX1END)) {
  720. /* One packet sent complete */
  721. db->tx_pkt_cnt--;
  722. dev->stats.tx_packets++;
  723. if (netif_msg_tx_done(db))
  724. dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
  725. /* Queue packet check & send */
  726. if (db->tx_pkt_cnt > 0)
  727. dm9000_send_packet(dev, db->queue_ip_summed,
  728. db->queue_pkt_len);
  729. netif_wake_queue(dev);
  730. }
  731. }
  732. struct dm9000_rxhdr {
  733. u8 RxPktReady;
  734. u8 RxStatus;
  735. __le16 RxLen;
  736. } __packed;
  737. /*
  738. * Received a packet and pass to upper layer
  739. */
  740. static void
  741. dm9000_rx(struct net_device *dev)
  742. {
  743. board_info_t *db = netdev_priv(dev);
  744. struct dm9000_rxhdr rxhdr;
  745. struct sk_buff *skb;
  746. u8 rxbyte, *rdptr;
  747. bool GoodPacket;
  748. int RxLen;
  749. /* Check packet ready or not */
  750. do {
  751. ior(db, DM9000_MRCMDX); /* Dummy read */
  752. /* Get most updated data */
  753. rxbyte = readb(db->io_data);
  754. /* Status check: this byte must be 0 or 1 */
  755. if (rxbyte & DM9000_PKT_ERR) {
  756. dev_warn(db->dev, "status check fail: %d\n", rxbyte);
  757. iow(db, DM9000_RCR, 0x00); /* Stop Device */
  758. iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
  759. return;
  760. }
  761. if (!(rxbyte & DM9000_PKT_RDY))
  762. return;
  763. /* A packet ready now & Get status/length */
  764. GoodPacket = true;
  765. writeb(DM9000_MRCMD, db->io_addr);
  766. (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
  767. RxLen = le16_to_cpu(rxhdr.RxLen);
  768. if (netif_msg_rx_status(db))
  769. dev_dbg(db->dev, "RX: status %02x, length %04x\n",
  770. rxhdr.RxStatus, RxLen);
  771. /* Packet Status check */
  772. if (RxLen < 0x40) {
  773. GoodPacket = false;
  774. if (netif_msg_rx_err(db))
  775. dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
  776. }
  777. if (RxLen > DM9000_PKT_MAX) {
  778. dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
  779. }
  780. /* rxhdr.RxStatus is identical to RSR register. */
  781. if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
  782. RSR_PLE | RSR_RWTO |
  783. RSR_LCS | RSR_RF)) {
  784. GoodPacket = false;
  785. if (rxhdr.RxStatus & RSR_FOE) {
  786. if (netif_msg_rx_err(db))
  787. dev_dbg(db->dev, "fifo error\n");
  788. dev->stats.rx_fifo_errors++;
  789. }
  790. if (rxhdr.RxStatus & RSR_CE) {
  791. if (netif_msg_rx_err(db))
  792. dev_dbg(db->dev, "crc error\n");
  793. dev->stats.rx_crc_errors++;
  794. }
  795. if (rxhdr.RxStatus & RSR_RF) {
  796. if (netif_msg_rx_err(db))
  797. dev_dbg(db->dev, "length error\n");
  798. dev->stats.rx_length_errors++;
  799. }
  800. }
  801. /* Move data from DM9000 */
  802. if (GoodPacket &&
  803. ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
  804. skb_reserve(skb, 2);
  805. rdptr = (u8 *) skb_put(skb, RxLen - 4);
  806. /* Read received packet from RX SRAM */
  807. (db->inblk)(db->io_data, rdptr, RxLen);
  808. dev->stats.rx_bytes += RxLen;
  809. /* Pass to upper layer */
  810. skb->protocol = eth_type_trans(skb, dev);
  811. if (dev->features & NETIF_F_RXCSUM) {
  812. if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
  813. skb->ip_summed = CHECKSUM_UNNECESSARY;
  814. else
  815. skb_checksum_none_assert(skb);
  816. }
  817. netif_rx(skb);
  818. dev->stats.rx_packets++;
  819. } else {
  820. /* need to dump the packet's data */
  821. (db->dumpblk)(db->io_data, RxLen);
  822. }
  823. } while (rxbyte & DM9000_PKT_RDY);
  824. }
  825. static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
  826. {
  827. struct net_device *dev = dev_id;
  828. board_info_t *db = netdev_priv(dev);
  829. int int_status;
  830. unsigned long flags;
  831. u8 reg_save;
  832. dm9000_dbg(db, 3, "entering %s\n", __func__);
  833. /* A real interrupt coming */
  834. /* holders of db->lock must always block IRQs */
  835. spin_lock_irqsave(&db->lock, flags);
  836. /* Save previous register address */
  837. reg_save = readb(db->io_addr);
  838. /* Disable all interrupts */
  839. iow(db, DM9000_IMR, IMR_PAR);
  840. /* Got DM9000 interrupt status */
  841. int_status = ior(db, DM9000_ISR); /* Got ISR */
  842. iow(db, DM9000_ISR, int_status); /* Clear ISR status */
  843. if (netif_msg_intr(db))
  844. dev_dbg(db->dev, "interrupt status %02x\n", int_status);
  845. /* Received the coming packet */
  846. if (int_status & ISR_PRS)
  847. dm9000_rx(dev);
  848. /* Trnasmit Interrupt check */
  849. if (int_status & ISR_PTS)
  850. dm9000_tx_done(dev, db);
  851. if (db->type != TYPE_DM9000E) {
  852. if (int_status & ISR_LNKCHNG) {
  853. /* fire a link-change request */
  854. schedule_delayed_work(&db->phy_poll, 1);
  855. }
  856. }
  857. /* Re-enable interrupt mask */
  858. iow(db, DM9000_IMR, db->imr_all);
  859. /* Restore previous register address */
  860. writeb(reg_save, db->io_addr);
  861. spin_unlock_irqrestore(&db->lock, flags);
  862. return IRQ_HANDLED;
  863. }
  864. static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id)
  865. {
  866. struct net_device *dev = dev_id;
  867. board_info_t *db = netdev_priv(dev);
  868. unsigned long flags;
  869. unsigned nsr, wcr;
  870. spin_lock_irqsave(&db->lock, flags);
  871. nsr = ior(db, DM9000_NSR);
  872. wcr = ior(db, DM9000_WCR);
  873. dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr);
  874. if (nsr & NSR_WAKEST) {
  875. /* clear, so we can avoid */
  876. iow(db, DM9000_NSR, NSR_WAKEST);
  877. if (wcr & WCR_LINKST)
  878. dev_info(db->dev, "wake by link status change\n");
  879. if (wcr & WCR_SAMPLEST)
  880. dev_info(db->dev, "wake by sample packet\n");
  881. if (wcr & WCR_MAGICST )
  882. dev_info(db->dev, "wake by magic packet\n");
  883. if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST)))
  884. dev_err(db->dev, "wake signalled with no reason? "
  885. "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr);
  886. }
  887. spin_unlock_irqrestore(&db->lock, flags);
  888. return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE;
  889. }
  890. #ifdef CONFIG_NET_POLL_CONTROLLER
  891. /*
  892. *Used by netconsole
  893. */
  894. static void dm9000_poll_controller(struct net_device *dev)
  895. {
  896. disable_irq(dev->irq);
  897. dm9000_interrupt(dev->irq, dev);
  898. enable_irq(dev->irq);
  899. }
  900. #endif
  901. /*
  902. * Open the interface.
  903. * The interface is opened whenever "ifconfig" actives it.
  904. */
  905. static int
  906. dm9000_open(struct net_device *dev)
  907. {
  908. board_info_t *db = netdev_priv(dev);
  909. unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
  910. if (netif_msg_ifup(db))
  911. dev_dbg(db->dev, "enabling %s\n", dev->name);
  912. /* If there is no IRQ type specified, default to something that
  913. * may work, and tell the user that this is a problem */
  914. if (irqflags == IRQF_TRIGGER_NONE)
  915. dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
  916. irqflags |= IRQF_SHARED;
  917. /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */
  918. iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
  919. mdelay(1); /* delay needs by DM9000B */
  920. /* Initialize DM9000 board */
  921. dm9000_reset(db);
  922. dm9000_init_dm9000(dev);
  923. if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev))
  924. return -EAGAIN;
  925. /* Init driver variable */
  926. db->dbug_cnt = 0;
  927. mii_check_media(&db->mii, netif_msg_link(db), 1);
  928. netif_start_queue(dev);
  929. dm9000_schedule_poll(db);
  930. return 0;
  931. }
  932. /*
  933. * Sleep, either by using msleep() or if we are suspending, then
  934. * use mdelay() to sleep.
  935. */
  936. static void dm9000_msleep(board_info_t *db, unsigned int ms)
  937. {
  938. if (db->in_suspend)
  939. mdelay(ms);
  940. else
  941. msleep(ms);
  942. }
  943. /*
  944. * Read a word from phyxcer
  945. */
  946. static int
  947. dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
  948. {
  949. board_info_t *db = netdev_priv(dev);
  950. unsigned long flags;
  951. unsigned int reg_save;
  952. int ret;
  953. mutex_lock(&db->addr_lock);
  954. spin_lock_irqsave(&db->lock,flags);
  955. /* Save previous register address */
  956. reg_save = readb(db->io_addr);
  957. /* Fill the phyxcer register into REG_0C */
  958. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  959. iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS); /* Issue phyxcer read command */
  960. writeb(reg_save, db->io_addr);
  961. spin_unlock_irqrestore(&db->lock,flags);
  962. dm9000_msleep(db, 1); /* Wait read complete */
  963. spin_lock_irqsave(&db->lock,flags);
  964. reg_save = readb(db->io_addr);
  965. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  966. /* The read data keeps on REG_0D & REG_0E */
  967. ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
  968. /* restore the previous address */
  969. writeb(reg_save, db->io_addr);
  970. spin_unlock_irqrestore(&db->lock,flags);
  971. mutex_unlock(&db->addr_lock);
  972. dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
  973. return ret;
  974. }
  975. /*
  976. * Write a word to phyxcer
  977. */
  978. static void
  979. dm9000_phy_write(struct net_device *dev,
  980. int phyaddr_unused, int reg, int value)
  981. {
  982. board_info_t *db = netdev_priv(dev);
  983. unsigned long flags;
  984. unsigned long reg_save;
  985. dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
  986. mutex_lock(&db->addr_lock);
  987. spin_lock_irqsave(&db->lock,flags);
  988. /* Save previous register address */
  989. reg_save = readb(db->io_addr);
  990. /* Fill the phyxcer register into REG_0C */
  991. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  992. /* Fill the written data into REG_0D & REG_0E */
  993. iow(db, DM9000_EPDRL, value);
  994. iow(db, DM9000_EPDRH, value >> 8);
  995. iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW); /* Issue phyxcer write command */
  996. writeb(reg_save, db->io_addr);
  997. spin_unlock_irqrestore(&db->lock, flags);
  998. dm9000_msleep(db, 1); /* Wait write complete */
  999. spin_lock_irqsave(&db->lock,flags);
  1000. reg_save = readb(db->io_addr);
  1001. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  1002. /* restore the previous address */
  1003. writeb(reg_save, db->io_addr);
  1004. spin_unlock_irqrestore(&db->lock, flags);
  1005. mutex_unlock(&db->addr_lock);
  1006. }
  1007. static void
  1008. dm9000_shutdown(struct net_device *dev)
  1009. {
  1010. board_info_t *db = netdev_priv(dev);
  1011. /* RESET device */
  1012. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  1013. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  1014. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  1015. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  1016. }
  1017. /*
  1018. * Stop the interface.
  1019. * The interface is stopped when it is brought.
  1020. */
  1021. static int
  1022. dm9000_stop(struct net_device *ndev)
  1023. {
  1024. board_info_t *db = netdev_priv(ndev);
  1025. if (netif_msg_ifdown(db))
  1026. dev_dbg(db->dev, "shutting down %s\n", ndev->name);
  1027. cancel_delayed_work_sync(&db->phy_poll);
  1028. netif_stop_queue(ndev);
  1029. netif_carrier_off(ndev);
  1030. /* free interrupt */
  1031. free_irq(ndev->irq, ndev);
  1032. dm9000_shutdown(ndev);
  1033. return 0;
  1034. }
  1035. static const struct net_device_ops dm9000_netdev_ops = {
  1036. .ndo_open = dm9000_open,
  1037. .ndo_stop = dm9000_stop,
  1038. .ndo_start_xmit = dm9000_start_xmit,
  1039. .ndo_tx_timeout = dm9000_timeout,
  1040. .ndo_set_multicast_list = dm9000_hash_table,
  1041. .ndo_do_ioctl = dm9000_ioctl,
  1042. .ndo_change_mtu = eth_change_mtu,
  1043. .ndo_set_features = dm9000_set_features,
  1044. .ndo_validate_addr = eth_validate_addr,
  1045. .ndo_set_mac_address = eth_mac_addr,
  1046. #ifdef CONFIG_NET_POLL_CONTROLLER
  1047. .ndo_poll_controller = dm9000_poll_controller,
  1048. #endif
  1049. };
  1050. /*
  1051. * Search DM9000 board, allocate space and register it
  1052. */
  1053. static int __devinit
  1054. dm9000_probe(struct platform_device *pdev)
  1055. {
  1056. struct dm9000_plat_data *pdata = pdev->dev.platform_data;
  1057. struct board_info *db; /* Point a board information structure */
  1058. struct net_device *ndev;
  1059. const unsigned char *mac_src;
  1060. int ret = 0;
  1061. int iosize;
  1062. int i;
  1063. u32 id_val;
  1064. /* Init network device */
  1065. ndev = alloc_etherdev(sizeof(struct board_info));
  1066. if (!ndev) {
  1067. dev_err(&pdev->dev, "could not allocate device.\n");
  1068. return -ENOMEM;
  1069. }
  1070. SET_NETDEV_DEV(ndev, &pdev->dev);
  1071. dev_dbg(&pdev->dev, "dm9000_probe()\n");
  1072. /* setup board info structure */
  1073. db = netdev_priv(ndev);
  1074. db->dev = &pdev->dev;
  1075. db->ndev = ndev;
  1076. spin_lock_init(&db->lock);
  1077. mutex_init(&db->addr_lock);
  1078. INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
  1079. db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1080. db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1081. db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1082. if (db->addr_res == NULL || db->data_res == NULL ||
  1083. db->irq_res == NULL) {
  1084. dev_err(db->dev, "insufficient resources\n");
  1085. ret = -ENOENT;
  1086. goto out;
  1087. }
  1088. db->irq_wake = platform_get_irq(pdev, 1);
  1089. if (db->irq_wake >= 0) {
  1090. dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);
  1091. ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
  1092. IRQF_SHARED, dev_name(db->dev), ndev);
  1093. if (ret) {
  1094. dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
  1095. } else {
  1096. /* test to see if irq is really wakeup capable */
  1097. ret = irq_set_irq_wake(db->irq_wake, 1);
  1098. if (ret) {
  1099. dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
  1100. db->irq_wake, ret);
  1101. ret = 0;
  1102. } else {
  1103. irq_set_irq_wake(db->irq_wake, 0);
  1104. db->wake_supported = 1;
  1105. }
  1106. }
  1107. }
  1108. iosize = resource_size(db->addr_res);
  1109. db->addr_req = request_mem_region(db->addr_res->start, iosize,
  1110. pdev->name);
  1111. if (db->addr_req == NULL) {
  1112. dev_err(db->dev, "cannot claim address reg area\n");
  1113. ret = -EIO;
  1114. goto out;
  1115. }
  1116. db->io_addr = ioremap(db->addr_res->start, iosize);
  1117. if (db->io_addr == NULL) {
  1118. dev_err(db->dev, "failed to ioremap address reg\n");
  1119. ret = -EINVAL;
  1120. goto out;
  1121. }
  1122. iosize = resource_size(db->data_res);
  1123. db->data_req = request_mem_region(db->data_res->start, iosize,
  1124. pdev->name);
  1125. if (db->data_req == NULL) {
  1126. dev_err(db->dev, "cannot claim data reg area\n");
  1127. ret = -EIO;
  1128. goto out;
  1129. }
  1130. db->io_data = ioremap(db->data_res->start, iosize);
  1131. if (db->io_data == NULL) {
  1132. dev_err(db->dev, "failed to ioremap data reg\n");
  1133. ret = -EINVAL;
  1134. goto out;
  1135. }
  1136. /* fill in parameters for net-dev structure */
  1137. ndev->base_addr = (unsigned long)db->io_addr;
  1138. ndev->irq = db->irq_res->start;
  1139. /* ensure at least we have a default set of IO routines */
  1140. dm9000_set_io(db, iosize);
  1141. /* check to see if anything is being over-ridden */
  1142. if (pdata != NULL) {
  1143. /* check to see if the driver wants to over-ride the
  1144. * default IO width */
  1145. if (pdata->flags & DM9000_PLATF_8BITONLY)
  1146. dm9000_set_io(db, 1);
  1147. if (pdata->flags & DM9000_PLATF_16BITONLY)
  1148. dm9000_set_io(db, 2);
  1149. if (pdata->flags & DM9000_PLATF_32BITONLY)
  1150. dm9000_set_io(db, 4);
  1151. /* check to see if there are any IO routine
  1152. * over-rides */
  1153. if (pdata->inblk != NULL)
  1154. db->inblk = pdata->inblk;
  1155. if (pdata->outblk != NULL)
  1156. db->outblk = pdata->outblk;
  1157. if (pdata->dumpblk != NULL)
  1158. db->dumpblk = pdata->dumpblk;
  1159. db->flags = pdata->flags;
  1160. }
  1161. #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
  1162. db->flags |= DM9000_PLATF_SIMPLE_PHY;
  1163. #endif
  1164. dm9000_reset(db);
  1165. /* try multiple times, DM9000 sometimes gets the read wrong */
  1166. for (i = 0; i < 8; i++) {
  1167. id_val = ior(db, DM9000_VIDL);
  1168. id_val |= (u32)ior(db, DM9000_VIDH) << 8;
  1169. id_val |= (u32)ior(db, DM9000_PIDL) << 16;
  1170. id_val |= (u32)ior(db, DM9000_PIDH) << 24;
  1171. if (id_val == DM9000_ID)
  1172. break;
  1173. dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
  1174. }
  1175. if (id_val != DM9000_ID) {
  1176. dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
  1177. ret = -ENODEV;
  1178. goto out;
  1179. }
  1180. /* Identify what type of DM9000 we are working on */
  1181. id_val = ior(db, DM9000_CHIPR);
  1182. dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
  1183. switch (id_val) {
  1184. case CHIPR_DM9000A:
  1185. db->type = TYPE_DM9000A;
  1186. break;
  1187. case CHIPR_DM9000B:
  1188. db->type = TYPE_DM9000B;
  1189. break;
  1190. default:
  1191. dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
  1192. db->type = TYPE_DM9000E;
  1193. }
  1194. /* dm9000a/b are capable of hardware checksum offload */
  1195. if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
  1196. ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
  1197. ndev->features |= ndev->hw_features;
  1198. }
  1199. /* from this point we assume that we have found a DM9000 */
  1200. /* driver system function */
  1201. ether_setup(ndev);
  1202. ndev->netdev_ops = &dm9000_netdev_ops;
  1203. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1204. ndev->ethtool_ops = &dm9000_ethtool_ops;
  1205. db->msg_enable = NETIF_MSG_LINK;
  1206. db->mii.phy_id_mask = 0x1f;
  1207. db->mii.reg_num_mask = 0x1f;
  1208. db->mii.force_media = 0;
  1209. db->mii.full_duplex = 0;
  1210. db->mii.dev = ndev;
  1211. db->mii.mdio_read = dm9000_phy_read;
  1212. db->mii.mdio_write = dm9000_phy_write;
  1213. mac_src = "eeprom";
  1214. /* try reading the node address from the attached EEPROM */
  1215. for (i = 0; i < 6; i += 2)
  1216. dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
  1217. if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
  1218. mac_src = "platform data";
  1219. memcpy(ndev->dev_addr, pdata->dev_addr, 6);
  1220. }
  1221. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1222. /* try reading from mac */
  1223. mac_src = "chip";
  1224. for (i = 0; i < 6; i++)
  1225. ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
  1226. }
  1227. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1228. dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
  1229. "set using ifconfig\n", ndev->name);
  1230. random_ether_addr(ndev->dev_addr);
  1231. mac_src = "random";
  1232. }
  1233. platform_set_drvdata(pdev, ndev);
  1234. ret = register_netdev(ndev);
  1235. if (ret == 0)
  1236. printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
  1237. ndev->name, dm9000_type_to_char(db->type),
  1238. db->io_addr, db->io_data, ndev->irq,
  1239. ndev->dev_addr, mac_src);
  1240. return 0;
  1241. out:
  1242. dev_err(db->dev, "not found (%d).\n", ret);
  1243. dm9000_release_board(pdev, db);
  1244. free_netdev(ndev);
  1245. return ret;
  1246. }
  1247. static int
  1248. dm9000_drv_suspend(struct device *dev)
  1249. {
  1250. struct platform_device *pdev = to_platform_device(dev);
  1251. struct net_device *ndev = platform_get_drvdata(pdev);
  1252. board_info_t *db;
  1253. if (ndev) {
  1254. db = netdev_priv(ndev);
  1255. db->in_suspend = 1;
  1256. if (!netif_running(ndev))
  1257. return 0;
  1258. netif_device_detach(ndev);
  1259. /* only shutdown if not using WoL */
  1260. if (!db->wake_state)
  1261. dm9000_shutdown(ndev);
  1262. }
  1263. return 0;
  1264. }
  1265. static int
  1266. dm9000_drv_resume(struct device *dev)
  1267. {
  1268. struct platform_device *pdev = to_platform_device(dev);
  1269. struct net_device *ndev = platform_get_drvdata(pdev);
  1270. board_info_t *db = netdev_priv(ndev);
  1271. if (ndev) {
  1272. if (netif_running(ndev)) {
  1273. /* reset if we were not in wake mode to ensure if
  1274. * the device was powered off it is in a known state */
  1275. if (!db->wake_state) {
  1276. dm9000_reset(db);
  1277. dm9000_init_dm9000(ndev);
  1278. }
  1279. netif_device_attach(ndev);
  1280. }
  1281. db->in_suspend = 0;
  1282. }
  1283. return 0;
  1284. }
  1285. static const struct dev_pm_ops dm9000_drv_pm_ops = {
  1286. .suspend = dm9000_drv_suspend,
  1287. .resume = dm9000_drv_resume,
  1288. };
  1289. static int __devexit
  1290. dm9000_drv_remove(struct platform_device *pdev)
  1291. {
  1292. struct net_device *ndev = platform_get_drvdata(pdev);
  1293. platform_set_drvdata(pdev, NULL);
  1294. unregister_netdev(ndev);
  1295. dm9000_release_board(pdev, netdev_priv(ndev));
  1296. free_netdev(ndev); /* free device structure */
  1297. dev_dbg(&pdev->dev, "released and freed device\n");
  1298. return 0;
  1299. }
  1300. static struct platform_driver dm9000_driver = {
  1301. .driver = {
  1302. .name = "dm9000",
  1303. .owner = THIS_MODULE,
  1304. .pm = &dm9000_drv_pm_ops,
  1305. },
  1306. .probe = dm9000_probe,
  1307. .remove = __devexit_p(dm9000_drv_remove),
  1308. };
  1309. static int __init
  1310. dm9000_init(void)
  1311. {
  1312. printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
  1313. return platform_driver_register(&dm9000_driver);
  1314. }
  1315. static void __exit
  1316. dm9000_cleanup(void)
  1317. {
  1318. platform_driver_unregister(&dm9000_driver);
  1319. }
  1320. module_init(dm9000_init);
  1321. module_exit(dm9000_cleanup);
  1322. MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
  1323. MODULE_DESCRIPTION("Davicom DM9000 network driver");
  1324. MODULE_LICENSE("GPL");
  1325. MODULE_ALIAS("platform:dm9000");