ks8851.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /* drivers/net/ks8851.c
  2. *
  3. * Copyright 2009 Simtec Electronics
  4. * http://www.simtec.co.uk/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #define DEBUG
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/cache.h>
  19. #include <linux/crc32.h>
  20. #include <linux/mii.h>
  21. #include <linux/spi/spi.h>
  22. #include "ks8851.h"
  23. /**
  24. * struct ks8851_rxctrl - KS8851 driver rx control
  25. * @mchash: Multicast hash-table data.
  26. * @rxcr1: KS_RXCR1 register setting
  27. * @rxcr2: KS_RXCR2 register setting
  28. *
  29. * Representation of the settings needs to control the receive filtering
  30. * such as the multicast hash-filter and the receive register settings. This
  31. * is used to make the job of working out if the receive settings change and
  32. * then issuing the new settings to the worker that will send the necessary
  33. * commands.
  34. */
  35. struct ks8851_rxctrl {
  36. u16 mchash[4];
  37. u16 rxcr1;
  38. u16 rxcr2;
  39. };
  40. /**
  41. * union ks8851_tx_hdr - tx header data
  42. * @txb: The header as bytes
  43. * @txw: The header as 16bit, little-endian words
  44. *
  45. * A dual representation of the tx header data to allow
  46. * access to individual bytes, and to allow 16bit accesses
  47. * with 16bit alignment.
  48. */
  49. union ks8851_tx_hdr {
  50. u8 txb[6];
  51. __le16 txw[3];
  52. };
  53. /**
  54. * struct ks8851_net - KS8851 driver private data
  55. * @netdev: The network device we're bound to
  56. * @spidev: The spi device we're bound to.
  57. * @lock: Lock to ensure that the device is not accessed when busy.
  58. * @statelock: Lock on this structure for tx list.
  59. * @mii: The MII state information for the mii calls.
  60. * @rxctrl: RX settings for @rxctrl_work.
  61. * @tx_work: Work queue for tx packets
  62. * @irq_work: Work queue for servicing interrupts
  63. * @rxctrl_work: Work queue for updating RX mode and multicast lists
  64. * @txq: Queue of packets for transmission.
  65. * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
  66. * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
  67. * @txh: Space for generating packet TX header in DMA-able data
  68. * @rxd: Space for receiving SPI data, in DMA-able space.
  69. * @txd: Space for transmitting SPI data, in DMA-able space.
  70. * @msg_enable: The message flags controlling driver output (see ethtool).
  71. * @fid: Incrementing frame id tag.
  72. * @rc_ier: Cached copy of KS_IER.
  73. * @rc_rxqcr: Cached copy of KS_RXQCR.
  74. *
  75. * The @lock ensures that the chip is protected when certain operations are
  76. * in progress. When the read or write packet transfer is in progress, most
  77. * of the chip registers are not ccessible until the transfer is finished and
  78. * the DMA has been de-asserted.
  79. *
  80. * The @statelock is used to protect information in the structure which may
  81. * need to be accessed via several sources, such as the network driver layer
  82. * or one of the work queues.
  83. *
  84. * We align the buffers we may use for rx/tx to ensure that if the SPI driver
  85. * wants to DMA map them, it will not have any problems with data the driver
  86. * modifies.
  87. */
  88. struct ks8851_net {
  89. struct net_device *netdev;
  90. struct spi_device *spidev;
  91. struct mutex lock;
  92. spinlock_t statelock;
  93. union ks8851_tx_hdr txh ____cacheline_aligned;
  94. u8 rxd[8];
  95. u8 txd[8];
  96. u32 msg_enable ____cacheline_aligned;
  97. u16 tx_space;
  98. u8 fid;
  99. u16 rc_ier;
  100. u16 rc_rxqcr;
  101. struct mii_if_info mii;
  102. struct ks8851_rxctrl rxctrl;
  103. struct work_struct tx_work;
  104. struct work_struct irq_work;
  105. struct work_struct rxctrl_work;
  106. struct sk_buff_head txq;
  107. struct spi_message spi_msg1;
  108. struct spi_message spi_msg2;
  109. struct spi_transfer spi_xfer1;
  110. struct spi_transfer spi_xfer2[2];
  111. };
  112. static int msg_enable;
  113. /* shift for byte-enable data */
  114. #define BYTE_EN(_x) ((_x) << 2)
  115. /* turn register number and byte-enable mask into data for start of packet */
  116. #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
  117. /* SPI register read/write calls.
  118. *
  119. * All these calls issue SPI transactions to access the chip's registers. They
  120. * all require that the necessary lock is held to prevent accesses when the
  121. * chip is busy transfering packet data (RX/TX FIFO accesses).
  122. */
  123. /**
  124. * ks8851_wrreg16 - write 16bit register value to chip
  125. * @ks: The chip state
  126. * @reg: The register address
  127. * @val: The value to write
  128. *
  129. * Issue a write to put the value @val into the register specified in @reg.
  130. */
  131. static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
  132. {
  133. struct spi_transfer *xfer = &ks->spi_xfer1;
  134. struct spi_message *msg = &ks->spi_msg1;
  135. __le16 txb[2];
  136. int ret;
  137. txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
  138. txb[1] = cpu_to_le16(val);
  139. xfer->tx_buf = txb;
  140. xfer->rx_buf = NULL;
  141. xfer->len = 4;
  142. ret = spi_sync(ks->spidev, msg);
  143. if (ret < 0)
  144. netdev_err(ks->netdev, "spi_sync() failed\n");
  145. }
  146. /**
  147. * ks8851_wrreg8 - write 8bit register value to chip
  148. * @ks: The chip state
  149. * @reg: The register address
  150. * @val: The value to write
  151. *
  152. * Issue a write to put the value @val into the register specified in @reg.
  153. */
  154. static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
  155. {
  156. struct spi_transfer *xfer = &ks->spi_xfer1;
  157. struct spi_message *msg = &ks->spi_msg1;
  158. __le16 txb[2];
  159. int ret;
  160. int bit;
  161. bit = 1 << (reg & 3);
  162. txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
  163. txb[1] = val;
  164. xfer->tx_buf = txb;
  165. xfer->rx_buf = NULL;
  166. xfer->len = 3;
  167. ret = spi_sync(ks->spidev, msg);
  168. if (ret < 0)
  169. netdev_err(ks->netdev, "spi_sync() failed\n");
  170. }
  171. /**
  172. * ks8851_rx_1msg - select whether to use one or two messages for spi read
  173. * @ks: The device structure
  174. *
  175. * Return whether to generate a single message with a tx and rx buffer
  176. * supplied to spi_sync(), or alternatively send the tx and rx buffers
  177. * as separate messages.
  178. *
  179. * Depending on the hardware in use, a single message may be more efficient
  180. * on interrupts or work done by the driver.
  181. *
  182. * This currently always returns true until we add some per-device data passed
  183. * from the platform code to specify which mode is better.
  184. */
  185. static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
  186. {
  187. return true;
  188. }
  189. /**
  190. * ks8851_rdreg - issue read register command and return the data
  191. * @ks: The device state
  192. * @op: The register address and byte enables in message format.
  193. * @rxb: The RX buffer to return the result into
  194. * @rxl: The length of data expected.
  195. *
  196. * This is the low level read call that issues the necessary spi message(s)
  197. * to read data from the register specified in @op.
  198. */
  199. static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
  200. u8 *rxb, unsigned rxl)
  201. {
  202. struct spi_transfer *xfer;
  203. struct spi_message *msg;
  204. __le16 *txb = (__le16 *)ks->txd;
  205. u8 *trx = ks->rxd;
  206. int ret;
  207. txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
  208. if (ks8851_rx_1msg(ks)) {
  209. msg = &ks->spi_msg1;
  210. xfer = &ks->spi_xfer1;
  211. xfer->tx_buf = txb;
  212. xfer->rx_buf = trx;
  213. xfer->len = rxl + 2;
  214. } else {
  215. msg = &ks->spi_msg2;
  216. xfer = ks->spi_xfer2;
  217. xfer->tx_buf = txb;
  218. xfer->rx_buf = NULL;
  219. xfer->len = 2;
  220. xfer++;
  221. xfer->tx_buf = NULL;
  222. xfer->rx_buf = trx;
  223. xfer->len = rxl;
  224. }
  225. ret = spi_sync(ks->spidev, msg);
  226. if (ret < 0)
  227. netdev_err(ks->netdev, "read: spi_sync() failed\n");
  228. else if (ks8851_rx_1msg(ks))
  229. memcpy(rxb, trx + 2, rxl);
  230. else
  231. memcpy(rxb, trx, rxl);
  232. }
  233. /**
  234. * ks8851_rdreg8 - read 8 bit register from device
  235. * @ks: The chip information
  236. * @reg: The register address
  237. *
  238. * Read a 8bit register from the chip, returning the result
  239. */
  240. static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
  241. {
  242. u8 rxb[1];
  243. ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
  244. return rxb[0];
  245. }
  246. /**
  247. * ks8851_rdreg16 - read 16 bit register from device
  248. * @ks: The chip information
  249. * @reg: The register address
  250. *
  251. * Read a 16bit register from the chip, returning the result
  252. */
  253. static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
  254. {
  255. __le16 rx = 0;
  256. ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
  257. return le16_to_cpu(rx);
  258. }
  259. /**
  260. * ks8851_rdreg32 - read 32 bit register from device
  261. * @ks: The chip information
  262. * @reg: The register address
  263. *
  264. * Read a 32bit register from the chip.
  265. *
  266. * Note, this read requires the address be aligned to 4 bytes.
  267. */
  268. static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
  269. {
  270. __le32 rx = 0;
  271. WARN_ON(reg & 3);
  272. ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
  273. return le32_to_cpu(rx);
  274. }
  275. /**
  276. * ks8851_soft_reset - issue one of the soft reset to the device
  277. * @ks: The device state.
  278. * @op: The bit(s) to set in the GRR
  279. *
  280. * Issue the relevant soft-reset command to the device's GRR register
  281. * specified by @op.
  282. *
  283. * Note, the delays are in there as a caution to ensure that the reset
  284. * has time to take effect and then complete. Since the datasheet does
  285. * not currently specify the exact sequence, we have chosen something
  286. * that seems to work with our device.
  287. */
  288. static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
  289. {
  290. ks8851_wrreg16(ks, KS_GRR, op);
  291. mdelay(1); /* wait a short time to effect reset */
  292. ks8851_wrreg16(ks, KS_GRR, 0);
  293. mdelay(1); /* wait for condition to clear */
  294. }
  295. /**
  296. * ks8851_write_mac_addr - write mac address to device registers
  297. * @dev: The network device
  298. *
  299. * Update the KS8851 MAC address registers from the address in @dev.
  300. *
  301. * This call assumes that the chip is not running, so there is no need to
  302. * shutdown the RXQ process whilst setting this.
  303. */
  304. static int ks8851_write_mac_addr(struct net_device *dev)
  305. {
  306. struct ks8851_net *ks = netdev_priv(dev);
  307. int i;
  308. mutex_lock(&ks->lock);
  309. for (i = 0; i < ETH_ALEN; i++)
  310. ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
  311. mutex_unlock(&ks->lock);
  312. return 0;
  313. }
  314. /**
  315. * ks8851_init_mac - initialise the mac address
  316. * @ks: The device structure
  317. *
  318. * Get or create the initial mac address for the device and then set that
  319. * into the station address register. Currently we assume that the device
  320. * does not have a valid mac address in it, and so we use random_ether_addr()
  321. * to create a new one.
  322. *
  323. * In future, the driver should check to see if the device has an EEPROM
  324. * attached and whether that has a valid ethernet address in it.
  325. */
  326. static void ks8851_init_mac(struct ks8851_net *ks)
  327. {
  328. struct net_device *dev = ks->netdev;
  329. random_ether_addr(dev->dev_addr);
  330. ks8851_write_mac_addr(dev);
  331. }
  332. /**
  333. * ks8851_irq - device interrupt handler
  334. * @irq: Interrupt number passed from the IRQ hnalder.
  335. * @pw: The private word passed to register_irq(), our struct ks8851_net.
  336. *
  337. * Disable the interrupt from happening again until we've processed the
  338. * current status by scheduling ks8851_irq_work().
  339. */
  340. static irqreturn_t ks8851_irq(int irq, void *pw)
  341. {
  342. struct ks8851_net *ks = pw;
  343. disable_irq_nosync(irq);
  344. schedule_work(&ks->irq_work);
  345. return IRQ_HANDLED;
  346. }
  347. /**
  348. * ks8851_rdfifo - read data from the receive fifo
  349. * @ks: The device state.
  350. * @buff: The buffer address
  351. * @len: The length of the data to read
  352. *
  353. * Issue an RXQ FIFO read command and read the @len amount of data from
  354. * the FIFO into the buffer specified by @buff.
  355. */
  356. static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
  357. {
  358. struct spi_transfer *xfer = ks->spi_xfer2;
  359. struct spi_message *msg = &ks->spi_msg2;
  360. u8 txb[1];
  361. int ret;
  362. netif_dbg(ks, rx_status, ks->netdev,
  363. "%s: %d@%p\n", __func__, len, buff);
  364. /* set the operation we're issuing */
  365. txb[0] = KS_SPIOP_RXFIFO;
  366. xfer->tx_buf = txb;
  367. xfer->rx_buf = NULL;
  368. xfer->len = 1;
  369. xfer++;
  370. xfer->rx_buf = buff;
  371. xfer->tx_buf = NULL;
  372. xfer->len = len;
  373. ret = spi_sync(ks->spidev, msg);
  374. if (ret < 0)
  375. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  376. }
  377. /**
  378. * ks8851_dbg_dumpkkt - dump initial packet contents to debug
  379. * @ks: The device state
  380. * @rxpkt: The data for the received packet
  381. *
  382. * Dump the initial data from the packet to dev_dbg().
  383. */
  384. static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
  385. {
  386. netdev_dbg(ks->netdev,
  387. "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
  388. rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
  389. rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
  390. rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
  391. }
  392. /**
  393. * ks8851_rx_pkts - receive packets from the host
  394. * @ks: The device information.
  395. *
  396. * This is called from the IRQ work queue when the system detects that there
  397. * are packets in the receive queue. Find out how many packets there are and
  398. * read them from the FIFO.
  399. */
  400. static void ks8851_rx_pkts(struct ks8851_net *ks)
  401. {
  402. struct sk_buff *skb;
  403. unsigned rxfc;
  404. unsigned rxlen;
  405. unsigned rxstat;
  406. u32 rxh;
  407. u8 *rxpkt;
  408. rxfc = ks8851_rdreg8(ks, KS_RXFC);
  409. netif_dbg(ks, rx_status, ks->netdev,
  410. "%s: %d packets\n", __func__, rxfc);
  411. /* Currently we're issuing a read per packet, but we could possibly
  412. * improve the code by issuing a single read, getting the receive
  413. * header, allocating the packet and then reading the packet data
  414. * out in one go.
  415. *
  416. * This form of operation would require us to hold the SPI bus'
  417. * chipselect low during the entie transaction to avoid any
  418. * reset to the data stream comming from the chip.
  419. */
  420. for (; rxfc != 0; rxfc--) {
  421. rxh = ks8851_rdreg32(ks, KS_RXFHSR);
  422. rxstat = rxh & 0xffff;
  423. rxlen = rxh >> 16;
  424. netif_dbg(ks, rx_status, ks->netdev,
  425. "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
  426. /* the length of the packet includes the 32bit CRC */
  427. /* set dma read address */
  428. ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
  429. /* start the packet dma process, and set auto-dequeue rx */
  430. ks8851_wrreg16(ks, KS_RXQCR,
  431. ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
  432. if (rxlen > 0) {
  433. skb = netdev_alloc_skb(ks->netdev, rxlen + 2 + 8);
  434. if (!skb) {
  435. /* todo - dump frame and move on */
  436. }
  437. /* two bytes to ensure ip is aligned, and four bytes
  438. * for the status header and 4 bytes of garbage */
  439. skb_reserve(skb, 2 + 4 + 4);
  440. rxpkt = skb_put(skb, rxlen - 4) - 8;
  441. /* align the packet length to 4 bytes, and add 4 bytes
  442. * as we're getting the rx status header as well */
  443. ks8851_rdfifo(ks, rxpkt, ALIGN(rxlen, 4) + 8);
  444. if (netif_msg_pktdata(ks))
  445. ks8851_dbg_dumpkkt(ks, rxpkt);
  446. skb->protocol = eth_type_trans(skb, ks->netdev);
  447. netif_rx(skb);
  448. ks->netdev->stats.rx_packets++;
  449. ks->netdev->stats.rx_bytes += rxlen - 4;
  450. }
  451. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  452. }
  453. }
  454. /**
  455. * ks8851_irq_work - work queue handler for dealing with interrupt requests
  456. * @work: The work structure that was scheduled by schedule_work()
  457. *
  458. * This is the handler invoked when the ks8851_irq() is called to find out
  459. * what happened, as we cannot allow ourselves to sleep whilst waiting for
  460. * anything other process has the chip's lock.
  461. *
  462. * Read the interrupt status, work out what needs to be done and then clear
  463. * any of the interrupts that are not needed.
  464. */
  465. static void ks8851_irq_work(struct work_struct *work)
  466. {
  467. struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
  468. unsigned status;
  469. unsigned handled = 0;
  470. mutex_lock(&ks->lock);
  471. status = ks8851_rdreg16(ks, KS_ISR);
  472. netif_dbg(ks, intr, ks->netdev,
  473. "%s: status 0x%04x\n", __func__, status);
  474. if (status & IRQ_LCI) {
  475. /* should do something about checking link status */
  476. handled |= IRQ_LCI;
  477. }
  478. if (status & IRQ_LDI) {
  479. u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
  480. pmecr &= ~PMECR_WKEVT_MASK;
  481. ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
  482. handled |= IRQ_LDI;
  483. }
  484. if (status & IRQ_RXPSI)
  485. handled |= IRQ_RXPSI;
  486. if (status & IRQ_TXI) {
  487. handled |= IRQ_TXI;
  488. /* no lock here, tx queue should have been stopped */
  489. /* update our idea of how much tx space is available to the
  490. * system */
  491. ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
  492. netif_dbg(ks, intr, ks->netdev,
  493. "%s: txspace %d\n", __func__, ks->tx_space);
  494. }
  495. if (status & IRQ_RXI)
  496. handled |= IRQ_RXI;
  497. if (status & IRQ_SPIBEI) {
  498. dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
  499. handled |= IRQ_SPIBEI;
  500. }
  501. ks8851_wrreg16(ks, KS_ISR, handled);
  502. if (status & IRQ_RXI) {
  503. /* the datasheet says to disable the rx interrupt during
  504. * packet read-out, however we're masking the interrupt
  505. * from the device so do not bother masking just the RX
  506. * from the device. */
  507. ks8851_rx_pkts(ks);
  508. }
  509. /* if something stopped the rx process, probably due to wanting
  510. * to change the rx settings, then do something about restarting
  511. * it. */
  512. if (status & IRQ_RXPSI) {
  513. struct ks8851_rxctrl *rxc = &ks->rxctrl;
  514. /* update the multicast hash table */
  515. ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
  516. ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
  517. ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
  518. ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
  519. ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
  520. ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
  521. }
  522. mutex_unlock(&ks->lock);
  523. if (status & IRQ_TXI)
  524. netif_wake_queue(ks->netdev);
  525. enable_irq(ks->netdev->irq);
  526. }
  527. /**
  528. * calc_txlen - calculate size of message to send packet
  529. * @len: Lenght of data
  530. *
  531. * Returns the size of the TXFIFO message needed to send
  532. * this packet.
  533. */
  534. static inline unsigned calc_txlen(unsigned len)
  535. {
  536. return ALIGN(len + 4, 4);
  537. }
  538. /**
  539. * ks8851_wrpkt - write packet to TX FIFO
  540. * @ks: The device state.
  541. * @txp: The sk_buff to transmit.
  542. * @irq: IRQ on completion of the packet.
  543. *
  544. * Send the @txp to the chip. This means creating the relevant packet header
  545. * specifying the length of the packet and the other information the chip
  546. * needs, such as IRQ on completion. Send the header and the packet data to
  547. * the device.
  548. */
  549. static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
  550. {
  551. struct spi_transfer *xfer = ks->spi_xfer2;
  552. struct spi_message *msg = &ks->spi_msg2;
  553. unsigned fid = 0;
  554. int ret;
  555. netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
  556. __func__, txp, txp->len, txp->data, irq);
  557. fid = ks->fid++;
  558. fid &= TXFR_TXFID_MASK;
  559. if (irq)
  560. fid |= TXFR_TXIC; /* irq on completion */
  561. /* start header at txb[1] to align txw entries */
  562. ks->txh.txb[1] = KS_SPIOP_TXFIFO;
  563. ks->txh.txw[1] = cpu_to_le16(fid);
  564. ks->txh.txw[2] = cpu_to_le16(txp->len);
  565. xfer->tx_buf = &ks->txh.txb[1];
  566. xfer->rx_buf = NULL;
  567. xfer->len = 5;
  568. xfer++;
  569. xfer->tx_buf = txp->data;
  570. xfer->rx_buf = NULL;
  571. xfer->len = ALIGN(txp->len, 4);
  572. ret = spi_sync(ks->spidev, msg);
  573. if (ret < 0)
  574. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  575. }
  576. /**
  577. * ks8851_done_tx - update and then free skbuff after transmitting
  578. * @ks: The device state
  579. * @txb: The buffer transmitted
  580. */
  581. static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
  582. {
  583. struct net_device *dev = ks->netdev;
  584. dev->stats.tx_bytes += txb->len;
  585. dev->stats.tx_packets++;
  586. dev_kfree_skb(txb);
  587. }
  588. /**
  589. * ks8851_tx_work - process tx packet(s)
  590. * @work: The work strucutre what was scheduled.
  591. *
  592. * This is called when a number of packets have been scheduled for
  593. * transmission and need to be sent to the device.
  594. */
  595. static void ks8851_tx_work(struct work_struct *work)
  596. {
  597. struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
  598. struct sk_buff *txb;
  599. bool last = skb_queue_empty(&ks->txq);
  600. mutex_lock(&ks->lock);
  601. while (!last) {
  602. txb = skb_dequeue(&ks->txq);
  603. last = skb_queue_empty(&ks->txq);
  604. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  605. ks8851_wrpkt(ks, txb, last);
  606. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  607. ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
  608. ks8851_done_tx(ks, txb);
  609. }
  610. mutex_unlock(&ks->lock);
  611. }
  612. /**
  613. * ks8851_set_powermode - set power mode of the device
  614. * @ks: The device state
  615. * @pwrmode: The power mode value to write to KS_PMECR.
  616. *
  617. * Change the power mode of the chip.
  618. */
  619. static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
  620. {
  621. unsigned pmecr;
  622. netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
  623. pmecr = ks8851_rdreg16(ks, KS_PMECR);
  624. pmecr &= ~PMECR_PM_MASK;
  625. pmecr |= pwrmode;
  626. ks8851_wrreg16(ks, KS_PMECR, pmecr);
  627. }
  628. /**
  629. * ks8851_net_open - open network device
  630. * @dev: The network device being opened.
  631. *
  632. * Called when the network device is marked active, such as a user executing
  633. * 'ifconfig up' on the device.
  634. */
  635. static int ks8851_net_open(struct net_device *dev)
  636. {
  637. struct ks8851_net *ks = netdev_priv(dev);
  638. /* lock the card, even if we may not actually be doing anything
  639. * else at the moment */
  640. mutex_lock(&ks->lock);
  641. netif_dbg(ks, ifup, ks->netdev, "opening\n");
  642. /* bring chip out of any power saving mode it was in */
  643. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  644. /* issue a soft reset to the RX/TX QMU to put it into a known
  645. * state. */
  646. ks8851_soft_reset(ks, GRR_QMU);
  647. /* setup transmission parameters */
  648. ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
  649. TXCR_TXPE | /* pad to min length */
  650. TXCR_TXCRC | /* add CRC */
  651. TXCR_TXFCE)); /* enable flow control */
  652. /* auto-increment tx data, reset tx pointer */
  653. ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
  654. /* setup receiver control */
  655. ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
  656. RXCR1_RXFCE | /* enable flow control */
  657. RXCR1_RXBE | /* broadcast enable */
  658. RXCR1_RXUE | /* unicast enable */
  659. RXCR1_RXE)); /* enable rx block */
  660. /* transfer entire frames out in one go */
  661. ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
  662. /* set receive counter timeouts */
  663. ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
  664. ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
  665. ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
  666. ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
  667. RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
  668. RXQCR_RXDTTE); /* IRQ on time exceeded */
  669. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  670. /* clear then enable interrupts */
  671. #define STD_IRQ (IRQ_LCI | /* Link Change */ \
  672. IRQ_TXI | /* TX done */ \
  673. IRQ_RXI | /* RX done */ \
  674. IRQ_SPIBEI | /* SPI bus error */ \
  675. IRQ_TXPSI | /* TX process stop */ \
  676. IRQ_RXPSI) /* RX process stop */
  677. ks->rc_ier = STD_IRQ;
  678. ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
  679. ks8851_wrreg16(ks, KS_IER, STD_IRQ);
  680. netif_start_queue(ks->netdev);
  681. netif_dbg(ks, ifup, ks->netdev, "network device up\n");
  682. mutex_unlock(&ks->lock);
  683. return 0;
  684. }
  685. /**
  686. * ks8851_net_stop - close network device
  687. * @dev: The device being closed.
  688. *
  689. * Called to close down a network device which has been active. Cancell any
  690. * work, shutdown the RX and TX process and then place the chip into a low
  691. * power state whilst it is not being used.
  692. */
  693. static int ks8851_net_stop(struct net_device *dev)
  694. {
  695. struct ks8851_net *ks = netdev_priv(dev);
  696. netif_info(ks, ifdown, dev, "shutting down\n");
  697. netif_stop_queue(dev);
  698. mutex_lock(&ks->lock);
  699. /* stop any outstanding work */
  700. flush_work(&ks->irq_work);
  701. flush_work(&ks->tx_work);
  702. flush_work(&ks->rxctrl_work);
  703. /* turn off the IRQs and ack any outstanding */
  704. ks8851_wrreg16(ks, KS_IER, 0x0000);
  705. ks8851_wrreg16(ks, KS_ISR, 0xffff);
  706. /* shutdown RX process */
  707. ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
  708. /* shutdown TX process */
  709. ks8851_wrreg16(ks, KS_TXCR, 0x0000);
  710. /* set powermode to soft power down to save power */
  711. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  712. /* ensure any queued tx buffers are dumped */
  713. while (!skb_queue_empty(&ks->txq)) {
  714. struct sk_buff *txb = skb_dequeue(&ks->txq);
  715. netif_dbg(ks, ifdown, ks->netdev,
  716. "%s: freeing txb %p\n", __func__, txb);
  717. dev_kfree_skb(txb);
  718. }
  719. mutex_unlock(&ks->lock);
  720. return 0;
  721. }
  722. /**
  723. * ks8851_start_xmit - transmit packet
  724. * @skb: The buffer to transmit
  725. * @dev: The device used to transmit the packet.
  726. *
  727. * Called by the network layer to transmit the @skb. Queue the packet for
  728. * the device and schedule the necessary work to transmit the packet when
  729. * it is free.
  730. *
  731. * We do this to firstly avoid sleeping with the network device locked,
  732. * and secondly so we can round up more than one packet to transmit which
  733. * means we can try and avoid generating too many transmit done interrupts.
  734. */
  735. static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
  736. struct net_device *dev)
  737. {
  738. struct ks8851_net *ks = netdev_priv(dev);
  739. unsigned needed = calc_txlen(skb->len);
  740. netdev_tx_t ret = NETDEV_TX_OK;
  741. netif_dbg(ks, tx_queued, ks->netdev,
  742. "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
  743. spin_lock(&ks->statelock);
  744. if (needed > ks->tx_space) {
  745. netif_stop_queue(dev);
  746. ret = NETDEV_TX_BUSY;
  747. } else {
  748. ks->tx_space -= needed;
  749. skb_queue_tail(&ks->txq, skb);
  750. }
  751. spin_unlock(&ks->statelock);
  752. schedule_work(&ks->tx_work);
  753. return ret;
  754. }
  755. /**
  756. * ks8851_rxctrl_work - work handler to change rx mode
  757. * @work: The work structure this belongs to.
  758. *
  759. * Lock the device and issue the necessary changes to the receive mode from
  760. * the network device layer. This is done so that we can do this without
  761. * having to sleep whilst holding the network device lock.
  762. *
  763. * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
  764. * receive parameters are programmed, we issue a write to disable the RXQ and
  765. * then wait for the interrupt handler to be triggered once the RXQ shutdown is
  766. * complete. The interrupt handler then writes the new values into the chip.
  767. */
  768. static void ks8851_rxctrl_work(struct work_struct *work)
  769. {
  770. struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
  771. mutex_lock(&ks->lock);
  772. /* need to shutdown RXQ before modifying filter parameters */
  773. ks8851_wrreg16(ks, KS_RXCR1, 0x00);
  774. mutex_unlock(&ks->lock);
  775. }
  776. static void ks8851_set_rx_mode(struct net_device *dev)
  777. {
  778. struct ks8851_net *ks = netdev_priv(dev);
  779. struct ks8851_rxctrl rxctrl;
  780. memset(&rxctrl, 0, sizeof(rxctrl));
  781. if (dev->flags & IFF_PROMISC) {
  782. /* interface to receive everything */
  783. rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
  784. } else if (dev->flags & IFF_ALLMULTI) {
  785. /* accept all multicast packets */
  786. rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
  787. RXCR1_RXPAFMA | RXCR1_RXMAFMA);
  788. } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
  789. struct dev_mc_list *mcptr;
  790. u32 crc;
  791. /* accept some multicast */
  792. netdev_for_each_mc_addr(mcptr, dev) {
  793. crc = ether_crc(ETH_ALEN, mcptr->dmi_addr);
  794. crc >>= (32 - 6); /* get top six bits */
  795. rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
  796. mcptr = mcptr->next;
  797. }
  798. rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
  799. } else {
  800. /* just accept broadcast / unicast */
  801. rxctrl.rxcr1 = RXCR1_RXPAFMA;
  802. }
  803. rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
  804. RXCR1_RXBE | /* broadcast enable */
  805. RXCR1_RXE | /* RX process enable */
  806. RXCR1_RXFCE); /* enable flow control */
  807. rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
  808. /* schedule work to do the actual set of the data if needed */
  809. spin_lock(&ks->statelock);
  810. if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
  811. memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
  812. schedule_work(&ks->rxctrl_work);
  813. }
  814. spin_unlock(&ks->statelock);
  815. }
  816. static int ks8851_set_mac_address(struct net_device *dev, void *addr)
  817. {
  818. struct sockaddr *sa = addr;
  819. if (netif_running(dev))
  820. return -EBUSY;
  821. if (!is_valid_ether_addr(sa->sa_data))
  822. return -EADDRNOTAVAIL;
  823. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  824. return ks8851_write_mac_addr(dev);
  825. }
  826. static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  827. {
  828. struct ks8851_net *ks = netdev_priv(dev);
  829. if (!netif_running(dev))
  830. return -EINVAL;
  831. return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
  832. }
  833. static const struct net_device_ops ks8851_netdev_ops = {
  834. .ndo_open = ks8851_net_open,
  835. .ndo_stop = ks8851_net_stop,
  836. .ndo_do_ioctl = ks8851_net_ioctl,
  837. .ndo_start_xmit = ks8851_start_xmit,
  838. .ndo_set_mac_address = ks8851_set_mac_address,
  839. .ndo_set_rx_mode = ks8851_set_rx_mode,
  840. .ndo_change_mtu = eth_change_mtu,
  841. .ndo_validate_addr = eth_validate_addr,
  842. };
  843. /* ethtool support */
  844. static void ks8851_get_drvinfo(struct net_device *dev,
  845. struct ethtool_drvinfo *di)
  846. {
  847. strlcpy(di->driver, "KS8851", sizeof(di->driver));
  848. strlcpy(di->version, "1.00", sizeof(di->version));
  849. strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
  850. }
  851. static u32 ks8851_get_msglevel(struct net_device *dev)
  852. {
  853. struct ks8851_net *ks = netdev_priv(dev);
  854. return ks->msg_enable;
  855. }
  856. static void ks8851_set_msglevel(struct net_device *dev, u32 to)
  857. {
  858. struct ks8851_net *ks = netdev_priv(dev);
  859. ks->msg_enable = to;
  860. }
  861. static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  862. {
  863. struct ks8851_net *ks = netdev_priv(dev);
  864. return mii_ethtool_gset(&ks->mii, cmd);
  865. }
  866. static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  867. {
  868. struct ks8851_net *ks = netdev_priv(dev);
  869. return mii_ethtool_sset(&ks->mii, cmd);
  870. }
  871. static u32 ks8851_get_link(struct net_device *dev)
  872. {
  873. struct ks8851_net *ks = netdev_priv(dev);
  874. return mii_link_ok(&ks->mii);
  875. }
  876. static int ks8851_nway_reset(struct net_device *dev)
  877. {
  878. struct ks8851_net *ks = netdev_priv(dev);
  879. return mii_nway_restart(&ks->mii);
  880. }
  881. static const struct ethtool_ops ks8851_ethtool_ops = {
  882. .get_drvinfo = ks8851_get_drvinfo,
  883. .get_msglevel = ks8851_get_msglevel,
  884. .set_msglevel = ks8851_set_msglevel,
  885. .get_settings = ks8851_get_settings,
  886. .set_settings = ks8851_set_settings,
  887. .get_link = ks8851_get_link,
  888. .nway_reset = ks8851_nway_reset,
  889. };
  890. /* MII interface controls */
  891. /**
  892. * ks8851_phy_reg - convert MII register into a KS8851 register
  893. * @reg: MII register number.
  894. *
  895. * Return the KS8851 register number for the corresponding MII PHY register
  896. * if possible. Return zero if the MII register has no direct mapping to the
  897. * KS8851 register set.
  898. */
  899. static int ks8851_phy_reg(int reg)
  900. {
  901. switch (reg) {
  902. case MII_BMCR:
  903. return KS_P1MBCR;
  904. case MII_BMSR:
  905. return KS_P1MBSR;
  906. case MII_PHYSID1:
  907. return KS_PHY1ILR;
  908. case MII_PHYSID2:
  909. return KS_PHY1IHR;
  910. case MII_ADVERTISE:
  911. return KS_P1ANAR;
  912. case MII_LPA:
  913. return KS_P1ANLPR;
  914. }
  915. return 0x0;
  916. }
  917. /**
  918. * ks8851_phy_read - MII interface PHY register read.
  919. * @dev: The network device the PHY is on.
  920. * @phy_addr: Address of PHY (ignored as we only have one)
  921. * @reg: The register to read.
  922. *
  923. * This call reads data from the PHY register specified in @reg. Since the
  924. * device does not support all the MII registers, the non-existant values
  925. * are always returned as zero.
  926. *
  927. * We return zero for unsupported registers as the MII code does not check
  928. * the value returned for any error status, and simply returns it to the
  929. * caller. The mii-tool that the driver was tested with takes any -ve error
  930. * as real PHY capabilities, thus displaying incorrect data to the user.
  931. */
  932. static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
  933. {
  934. struct ks8851_net *ks = netdev_priv(dev);
  935. int ksreg;
  936. int result;
  937. ksreg = ks8851_phy_reg(reg);
  938. if (!ksreg)
  939. return 0x0; /* no error return allowed, so use zero */
  940. mutex_lock(&ks->lock);
  941. result = ks8851_rdreg16(ks, ksreg);
  942. mutex_unlock(&ks->lock);
  943. return result;
  944. }
  945. static void ks8851_phy_write(struct net_device *dev,
  946. int phy, int reg, int value)
  947. {
  948. struct ks8851_net *ks = netdev_priv(dev);
  949. int ksreg;
  950. ksreg = ks8851_phy_reg(reg);
  951. if (ksreg) {
  952. mutex_lock(&ks->lock);
  953. ks8851_wrreg16(ks, ksreg, value);
  954. mutex_unlock(&ks->lock);
  955. }
  956. }
  957. /**
  958. * ks8851_read_selftest - read the selftest memory info.
  959. * @ks: The device state
  960. *
  961. * Read and check the TX/RX memory selftest information.
  962. */
  963. static int ks8851_read_selftest(struct ks8851_net *ks)
  964. {
  965. unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
  966. int ret = 0;
  967. unsigned rd;
  968. rd = ks8851_rdreg16(ks, KS_MBIR);
  969. if ((rd & both_done) != both_done) {
  970. netdev_warn(ks->netdev, "Memory selftest not finished\n");
  971. return 0;
  972. }
  973. if (rd & MBIR_TXMBFA) {
  974. netdev_err(ks->netdev, "TX memory selftest fail\n");
  975. ret |= 1;
  976. }
  977. if (rd & MBIR_RXMBFA) {
  978. netdev_err(ks->netdev, "RX memory selftest fail\n");
  979. ret |= 2;
  980. }
  981. return 0;
  982. }
  983. /* driver bus management functions */
  984. static int __devinit ks8851_probe(struct spi_device *spi)
  985. {
  986. struct net_device *ndev;
  987. struct ks8851_net *ks;
  988. int ret;
  989. ndev = alloc_etherdev(sizeof(struct ks8851_net));
  990. if (!ndev) {
  991. dev_err(&spi->dev, "failed to alloc ethernet device\n");
  992. return -ENOMEM;
  993. }
  994. spi->bits_per_word = 8;
  995. ks = netdev_priv(ndev);
  996. ks->netdev = ndev;
  997. ks->spidev = spi;
  998. ks->tx_space = 6144;
  999. mutex_init(&ks->lock);
  1000. spin_lock_init(&ks->statelock);
  1001. INIT_WORK(&ks->tx_work, ks8851_tx_work);
  1002. INIT_WORK(&ks->irq_work, ks8851_irq_work);
  1003. INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
  1004. /* initialise pre-made spi transfer messages */
  1005. spi_message_init(&ks->spi_msg1);
  1006. spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
  1007. spi_message_init(&ks->spi_msg2);
  1008. spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
  1009. spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
  1010. /* setup mii state */
  1011. ks->mii.dev = ndev;
  1012. ks->mii.phy_id = 1,
  1013. ks->mii.phy_id_mask = 1;
  1014. ks->mii.reg_num_mask = 0xf;
  1015. ks->mii.mdio_read = ks8851_phy_read;
  1016. ks->mii.mdio_write = ks8851_phy_write;
  1017. dev_info(&spi->dev, "message enable is %d\n", msg_enable);
  1018. /* set the default message enable */
  1019. ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
  1020. NETIF_MSG_PROBE |
  1021. NETIF_MSG_LINK));
  1022. skb_queue_head_init(&ks->txq);
  1023. SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
  1024. SET_NETDEV_DEV(ndev, &spi->dev);
  1025. dev_set_drvdata(&spi->dev, ks);
  1026. ndev->if_port = IF_PORT_100BASET;
  1027. ndev->netdev_ops = &ks8851_netdev_ops;
  1028. ndev->irq = spi->irq;
  1029. /* issue a global soft reset to reset the device. */
  1030. ks8851_soft_reset(ks, GRR_GSR);
  1031. /* simple check for a valid chip being connected to the bus */
  1032. if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
  1033. dev_err(&spi->dev, "failed to read device ID\n");
  1034. ret = -ENODEV;
  1035. goto err_id;
  1036. }
  1037. ks8851_read_selftest(ks);
  1038. ks8851_init_mac(ks);
  1039. ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
  1040. ndev->name, ks);
  1041. if (ret < 0) {
  1042. dev_err(&spi->dev, "failed to get irq\n");
  1043. goto err_irq;
  1044. }
  1045. ret = register_netdev(ndev);
  1046. if (ret) {
  1047. dev_err(&spi->dev, "failed to register network device\n");
  1048. goto err_netdev;
  1049. }
  1050. netdev_info(ndev, "revision %d, MAC %pM, IRQ %d\n",
  1051. CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
  1052. ndev->dev_addr, ndev->irq);
  1053. return 0;
  1054. err_netdev:
  1055. free_irq(ndev->irq, ndev);
  1056. err_id:
  1057. err_irq:
  1058. free_netdev(ndev);
  1059. return ret;
  1060. }
  1061. static int __devexit ks8851_remove(struct spi_device *spi)
  1062. {
  1063. struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
  1064. if (netif_msg_drv(priv))
  1065. dev_info(&spi->dev, "remove\n");
  1066. unregister_netdev(priv->netdev);
  1067. free_irq(spi->irq, priv);
  1068. free_netdev(priv->netdev);
  1069. return 0;
  1070. }
  1071. static struct spi_driver ks8851_driver = {
  1072. .driver = {
  1073. .name = "ks8851",
  1074. .owner = THIS_MODULE,
  1075. },
  1076. .probe = ks8851_probe,
  1077. .remove = __devexit_p(ks8851_remove),
  1078. };
  1079. static int __init ks8851_init(void)
  1080. {
  1081. return spi_register_driver(&ks8851_driver);
  1082. }
  1083. static void __exit ks8851_exit(void)
  1084. {
  1085. spi_unregister_driver(&ks8851_driver);
  1086. }
  1087. module_init(ks8851_init);
  1088. module_exit(ks8851_exit);
  1089. MODULE_DESCRIPTION("KS8851 Network driver");
  1090. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1091. MODULE_LICENSE("GPL");
  1092. module_param_named(message, msg_enable, int, 0);
  1093. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
  1094. MODULE_ALIAS("spi:ks8851");