dm9000.c 40 KB

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