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