dm9000.c 38 KB

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