ks8851.c 42 KB

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