ks8851.c 40 KB

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