ks8851.c 34 KB

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