dm9000.c 39 KB

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