dm9000.c 38 KB

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