ks8851.c 39 KB

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