dm9000.c 38 KB

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