ks8851.c 35 KB

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