dm9000.c 38 KB

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