dm9000.c 39 KB

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