dm9000.c 33 KB

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