cpmac.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * Copyright (C) 2006, 2007 Eugene Konev
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/errno.h>
  25. #include <linux/types.h>
  26. #include <linux/delay.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/ethtool.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/mii.h>
  33. #include <linux/phy.h>
  34. #include <linux/phy_fixed.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/clk.h>
  38. #include <linux/gpio.h>
  39. #include <linux/atomic.h>
  40. #include <asm/mach-ar7/ar7.h>
  41. MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
  42. MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
  43. MODULE_LICENSE("GPL");
  44. MODULE_ALIAS("platform:cpmac");
  45. static int debug_level = 8;
  46. static int dumb_switch;
  47. /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
  48. module_param(debug_level, int, 0444);
  49. module_param(dumb_switch, int, 0444);
  50. MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
  51. MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
  52. #define CPMAC_VERSION "0.5.2"
  53. /* frame size + 802.1q tag + FCS size */
  54. #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
  55. #define CPMAC_QUEUES 8
  56. /* Ethernet registers */
  57. #define CPMAC_TX_CONTROL 0x0004
  58. #define CPMAC_TX_TEARDOWN 0x0008
  59. #define CPMAC_RX_CONTROL 0x0014
  60. #define CPMAC_RX_TEARDOWN 0x0018
  61. #define CPMAC_MBP 0x0100
  62. #define MBP_RXPASSCRC 0x40000000
  63. #define MBP_RXQOS 0x20000000
  64. #define MBP_RXNOCHAIN 0x10000000
  65. #define MBP_RXCMF 0x01000000
  66. #define MBP_RXSHORT 0x00800000
  67. #define MBP_RXCEF 0x00400000
  68. #define MBP_RXPROMISC 0x00200000
  69. #define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
  70. #define MBP_RXBCAST 0x00002000
  71. #define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
  72. #define MBP_RXMCAST 0x00000020
  73. #define MBP_MCASTCHAN(channel) ((channel) & 0x7)
  74. #define CPMAC_UNICAST_ENABLE 0x0104
  75. #define CPMAC_UNICAST_CLEAR 0x0108
  76. #define CPMAC_MAX_LENGTH 0x010c
  77. #define CPMAC_BUFFER_OFFSET 0x0110
  78. #define CPMAC_MAC_CONTROL 0x0160
  79. #define MAC_TXPTYPE 0x00000200
  80. #define MAC_TXPACE 0x00000040
  81. #define MAC_MII 0x00000020
  82. #define MAC_TXFLOW 0x00000010
  83. #define MAC_RXFLOW 0x00000008
  84. #define MAC_MTEST 0x00000004
  85. #define MAC_LOOPBACK 0x00000002
  86. #define MAC_FDX 0x00000001
  87. #define CPMAC_MAC_STATUS 0x0164
  88. #define MAC_STATUS_QOS 0x00000004
  89. #define MAC_STATUS_RXFLOW 0x00000002
  90. #define MAC_STATUS_TXFLOW 0x00000001
  91. #define CPMAC_TX_INT_ENABLE 0x0178
  92. #define CPMAC_TX_INT_CLEAR 0x017c
  93. #define CPMAC_MAC_INT_VECTOR 0x0180
  94. #define MAC_INT_STATUS 0x00080000
  95. #define MAC_INT_HOST 0x00040000
  96. #define MAC_INT_RX 0x00020000
  97. #define MAC_INT_TX 0x00010000
  98. #define CPMAC_MAC_EOI_VECTOR 0x0184
  99. #define CPMAC_RX_INT_ENABLE 0x0198
  100. #define CPMAC_RX_INT_CLEAR 0x019c
  101. #define CPMAC_MAC_INT_ENABLE 0x01a8
  102. #define CPMAC_MAC_INT_CLEAR 0x01ac
  103. #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
  104. #define CPMAC_MAC_ADDR_MID 0x01d0
  105. #define CPMAC_MAC_ADDR_HI 0x01d4
  106. #define CPMAC_MAC_HASH_LO 0x01d8
  107. #define CPMAC_MAC_HASH_HI 0x01dc
  108. #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
  109. #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
  110. #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
  111. #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
  112. #define CPMAC_REG_END 0x0680
  113. /* Rx/Tx statistics
  114. * TODO: use some of them to fill stats in cpmac_stats()
  115. */
  116. #define CPMAC_STATS_RX_GOOD 0x0200
  117. #define CPMAC_STATS_RX_BCAST 0x0204
  118. #define CPMAC_STATS_RX_MCAST 0x0208
  119. #define CPMAC_STATS_RX_PAUSE 0x020c
  120. #define CPMAC_STATS_RX_CRC 0x0210
  121. #define CPMAC_STATS_RX_ALIGN 0x0214
  122. #define CPMAC_STATS_RX_OVER 0x0218
  123. #define CPMAC_STATS_RX_JABBER 0x021c
  124. #define CPMAC_STATS_RX_UNDER 0x0220
  125. #define CPMAC_STATS_RX_FRAG 0x0224
  126. #define CPMAC_STATS_RX_FILTER 0x0228
  127. #define CPMAC_STATS_RX_QOSFILTER 0x022c
  128. #define CPMAC_STATS_RX_OCTETS 0x0230
  129. #define CPMAC_STATS_TX_GOOD 0x0234
  130. #define CPMAC_STATS_TX_BCAST 0x0238
  131. #define CPMAC_STATS_TX_MCAST 0x023c
  132. #define CPMAC_STATS_TX_PAUSE 0x0240
  133. #define CPMAC_STATS_TX_DEFER 0x0244
  134. #define CPMAC_STATS_TX_COLLISION 0x0248
  135. #define CPMAC_STATS_TX_SINGLECOLL 0x024c
  136. #define CPMAC_STATS_TX_MULTICOLL 0x0250
  137. #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
  138. #define CPMAC_STATS_TX_LATECOLL 0x0258
  139. #define CPMAC_STATS_TX_UNDERRUN 0x025c
  140. #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
  141. #define CPMAC_STATS_TX_OCTETS 0x0264
  142. #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
  143. #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
  144. (reg)))
  145. /* MDIO bus */
  146. #define CPMAC_MDIO_VERSION 0x0000
  147. #define CPMAC_MDIO_CONTROL 0x0004
  148. #define MDIOC_IDLE 0x80000000
  149. #define MDIOC_ENABLE 0x40000000
  150. #define MDIOC_PREAMBLE 0x00100000
  151. #define MDIOC_FAULT 0x00080000
  152. #define MDIOC_FAULTDETECT 0x00040000
  153. #define MDIOC_INTTEST 0x00020000
  154. #define MDIOC_CLKDIV(div) ((div) & 0xff)
  155. #define CPMAC_MDIO_ALIVE 0x0008
  156. #define CPMAC_MDIO_LINK 0x000c
  157. #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
  158. #define MDIO_BUSY 0x80000000
  159. #define MDIO_WRITE 0x40000000
  160. #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
  161. #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
  162. #define MDIO_DATA(data) ((data) & 0xffff)
  163. #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
  164. #define PHYSEL_LINKSEL 0x00000040
  165. #define PHYSEL_LINKINT 0x00000020
  166. struct cpmac_desc {
  167. u32 hw_next;
  168. u32 hw_data;
  169. u16 buflen;
  170. u16 bufflags;
  171. u16 datalen;
  172. u16 dataflags;
  173. #define CPMAC_SOP 0x8000
  174. #define CPMAC_EOP 0x4000
  175. #define CPMAC_OWN 0x2000
  176. #define CPMAC_EOQ 0x1000
  177. struct sk_buff *skb;
  178. struct cpmac_desc *next;
  179. struct cpmac_desc *prev;
  180. dma_addr_t mapping;
  181. dma_addr_t data_mapping;
  182. };
  183. struct cpmac_priv {
  184. spinlock_t lock;
  185. spinlock_t rx_lock;
  186. struct cpmac_desc *rx_head;
  187. int ring_size;
  188. struct cpmac_desc *desc_ring;
  189. dma_addr_t dma_ring;
  190. void __iomem *regs;
  191. struct mii_bus *mii_bus;
  192. char phy_name[MII_BUS_ID_SIZE + 3];
  193. int oldlink, oldspeed, oldduplex;
  194. u32 msg_enable;
  195. struct net_device *dev;
  196. struct work_struct reset_work;
  197. struct platform_device *pdev;
  198. struct napi_struct napi;
  199. atomic_t reset_pending;
  200. };
  201. static irqreturn_t cpmac_irq(int, void *);
  202. static void cpmac_hw_start(struct net_device *dev);
  203. static void cpmac_hw_stop(struct net_device *dev);
  204. static int cpmac_stop(struct net_device *dev);
  205. static int cpmac_open(struct net_device *dev);
  206. static void cpmac_dump_regs(struct net_device *dev)
  207. {
  208. int i;
  209. struct cpmac_priv *priv = netdev_priv(dev);
  210. for (i = 0; i < CPMAC_REG_END; i += 4) {
  211. if (i % 16 == 0) {
  212. if (i)
  213. printk("\n");
  214. printk("%s: reg[%p]:", dev->name, priv->regs + i);
  215. }
  216. printk(" %08x", cpmac_read(priv->regs, i));
  217. }
  218. printk("\n");
  219. }
  220. static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
  221. {
  222. int i;
  223. printk("%s: desc[%p]:", dev->name, desc);
  224. for (i = 0; i < sizeof(*desc) / 4; i++)
  225. printk(" %08x", ((u32 *)desc)[i]);
  226. printk("\n");
  227. }
  228. static void cpmac_dump_all_desc(struct net_device *dev)
  229. {
  230. struct cpmac_priv *priv = netdev_priv(dev);
  231. struct cpmac_desc *dump = priv->rx_head;
  232. do {
  233. cpmac_dump_desc(dev, dump);
  234. dump = dump->next;
  235. } while (dump != priv->rx_head);
  236. }
  237. static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
  238. {
  239. int i;
  240. printk("%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
  241. for (i = 0; i < skb->len; i++) {
  242. if (i % 16 == 0) {
  243. if (i)
  244. printk("\n");
  245. printk("%s: data[%p]:", dev->name, skb->data + i);
  246. }
  247. printk(" %02x", ((u8 *)skb->data)[i]);
  248. }
  249. printk("\n");
  250. }
  251. static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  252. {
  253. u32 val;
  254. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  255. cpu_relax();
  256. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
  257. MDIO_PHY(phy_id));
  258. while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
  259. cpu_relax();
  260. return MDIO_DATA(val);
  261. }
  262. static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
  263. int reg, u16 val)
  264. {
  265. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  266. cpu_relax();
  267. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
  268. MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
  269. return 0;
  270. }
  271. static int cpmac_mdio_reset(struct mii_bus *bus)
  272. {
  273. struct clk *cpmac_clk;
  274. cpmac_clk = clk_get(&bus->dev, "cpmac");
  275. if (IS_ERR(cpmac_clk)) {
  276. pr_err("unable to get cpmac clock\n");
  277. return -1;
  278. }
  279. ar7_device_reset(AR7_RESET_BIT_MDIO);
  280. cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
  281. MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
  282. return 0;
  283. }
  284. static struct mii_bus *cpmac_mii;
  285. static void cpmac_set_multicast_list(struct net_device *dev)
  286. {
  287. struct netdev_hw_addr *ha;
  288. u8 tmp;
  289. u32 mbp, bit, hash[2] = { 0, };
  290. struct cpmac_priv *priv = netdev_priv(dev);
  291. mbp = cpmac_read(priv->regs, CPMAC_MBP);
  292. if (dev->flags & IFF_PROMISC) {
  293. cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
  294. MBP_RXPROMISC);
  295. } else {
  296. cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
  297. if (dev->flags & IFF_ALLMULTI) {
  298. /* enable all multicast mode */
  299. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
  300. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
  301. } else {
  302. /* cpmac uses some strange mac address hashing
  303. * (not crc32)
  304. */
  305. netdev_for_each_mc_addr(ha, dev) {
  306. bit = 0;
  307. tmp = ha->addr[0];
  308. bit ^= (tmp >> 2) ^ (tmp << 4);
  309. tmp = ha->addr[1];
  310. bit ^= (tmp >> 4) ^ (tmp << 2);
  311. tmp = ha->addr[2];
  312. bit ^= (tmp >> 6) ^ tmp;
  313. tmp = ha->addr[3];
  314. bit ^= (tmp >> 2) ^ (tmp << 4);
  315. tmp = ha->addr[4];
  316. bit ^= (tmp >> 4) ^ (tmp << 2);
  317. tmp = ha->addr[5];
  318. bit ^= (tmp >> 6) ^ tmp;
  319. bit &= 0x3f;
  320. hash[bit / 32] |= 1 << (bit % 32);
  321. }
  322. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
  323. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
  324. }
  325. }
  326. }
  327. static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
  328. struct cpmac_desc *desc)
  329. {
  330. struct sk_buff *skb, *result = NULL;
  331. if (unlikely(netif_msg_hw(priv)))
  332. cpmac_dump_desc(priv->dev, desc);
  333. cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
  334. if (unlikely(!desc->datalen)) {
  335. if (netif_msg_rx_err(priv) && net_ratelimit())
  336. netdev_warn(priv->dev, "rx: spurious interrupt\n");
  337. return NULL;
  338. }
  339. skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
  340. if (likely(skb)) {
  341. skb_put(desc->skb, desc->datalen);
  342. desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
  343. skb_checksum_none_assert(desc->skb);
  344. priv->dev->stats.rx_packets++;
  345. priv->dev->stats.rx_bytes += desc->datalen;
  346. result = desc->skb;
  347. dma_unmap_single(&priv->dev->dev, desc->data_mapping,
  348. CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
  349. desc->skb = skb;
  350. desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
  351. CPMAC_SKB_SIZE,
  352. DMA_FROM_DEVICE);
  353. desc->hw_data = (u32)desc->data_mapping;
  354. if (unlikely(netif_msg_pktdata(priv))) {
  355. netdev_dbg(priv->dev, "received packet:\n");
  356. cpmac_dump_skb(priv->dev, result);
  357. }
  358. } else {
  359. if (netif_msg_rx_err(priv) && net_ratelimit())
  360. netdev_warn(priv->dev,
  361. "low on skbs, dropping packet\n");
  362. priv->dev->stats.rx_dropped++;
  363. }
  364. desc->buflen = CPMAC_SKB_SIZE;
  365. desc->dataflags = CPMAC_OWN;
  366. return result;
  367. }
  368. static int cpmac_poll(struct napi_struct *napi, int budget)
  369. {
  370. struct sk_buff *skb;
  371. struct cpmac_desc *desc, *restart;
  372. struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
  373. int received = 0, processed = 0;
  374. spin_lock(&priv->rx_lock);
  375. if (unlikely(!priv->rx_head)) {
  376. if (netif_msg_rx_err(priv) && net_ratelimit())
  377. netdev_warn(priv->dev, "rx: polling, but no queue\n");
  378. spin_unlock(&priv->rx_lock);
  379. napi_complete(napi);
  380. return 0;
  381. }
  382. desc = priv->rx_head;
  383. restart = NULL;
  384. while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
  385. processed++;
  386. if ((desc->dataflags & CPMAC_EOQ) != 0) {
  387. /* The last update to eoq->hw_next didn't happen
  388. * soon enough, and the receiver stopped here.
  389. * Remember this descriptor so we can restart
  390. * the receiver after freeing some space.
  391. */
  392. if (unlikely(restart)) {
  393. if (netif_msg_rx_err(priv))
  394. netdev_err(priv->dev, "poll found a"
  395. " duplicate EOQ: %p and %p\n",
  396. restart, desc);
  397. goto fatal_error;
  398. }
  399. restart = desc->next;
  400. }
  401. skb = cpmac_rx_one(priv, desc);
  402. if (likely(skb)) {
  403. netif_receive_skb(skb);
  404. received++;
  405. }
  406. desc = desc->next;
  407. }
  408. if (desc != priv->rx_head) {
  409. /* We freed some buffers, but not the whole ring,
  410. * add what we did free to the rx list
  411. */
  412. desc->prev->hw_next = (u32)0;
  413. priv->rx_head->prev->hw_next = priv->rx_head->mapping;
  414. }
  415. /* Optimization: If we did not actually process an EOQ (perhaps because
  416. * of quota limits), check to see if the tail of the queue has EOQ set.
  417. * We should immediately restart in that case so that the receiver can
  418. * restart and run in parallel with more packet processing.
  419. * This lets us handle slightly larger bursts before running
  420. * out of ring space (assuming dev->weight < ring_size)
  421. */
  422. if (!restart &&
  423. (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
  424. == CPMAC_EOQ &&
  425. (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
  426. /* reset EOQ so the poll loop (above) doesn't try to
  427. * restart this when it eventually gets to this descriptor.
  428. */
  429. priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
  430. restart = priv->rx_head;
  431. }
  432. if (restart) {
  433. priv->dev->stats.rx_errors++;
  434. priv->dev->stats.rx_fifo_errors++;
  435. if (netif_msg_rx_err(priv) && net_ratelimit())
  436. netdev_warn(priv->dev, "rx dma ring overrun\n");
  437. if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
  438. if (netif_msg_drv(priv))
  439. netdev_err(priv->dev, "cpmac_poll is trying "
  440. "to restart rx from a descriptor "
  441. "that's not free: %p\n", restart);
  442. goto fatal_error;
  443. }
  444. cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
  445. }
  446. priv->rx_head = desc;
  447. spin_unlock(&priv->rx_lock);
  448. if (unlikely(netif_msg_rx_status(priv)))
  449. netdev_dbg(priv->dev, "poll processed %d packets\n", received);
  450. if (processed == 0) {
  451. /* we ran out of packets to read,
  452. * revert to interrupt-driven mode
  453. */
  454. napi_complete(napi);
  455. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  456. return 0;
  457. }
  458. return 1;
  459. fatal_error:
  460. /* Something went horribly wrong.
  461. * Reset hardware to try to recover rather than wedging.
  462. */
  463. if (netif_msg_drv(priv)) {
  464. netdev_err(priv->dev, "cpmac_poll is confused. "
  465. "Resetting hardware\n");
  466. cpmac_dump_all_desc(priv->dev);
  467. netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
  468. cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
  469. cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
  470. }
  471. spin_unlock(&priv->rx_lock);
  472. napi_complete(napi);
  473. netif_tx_stop_all_queues(priv->dev);
  474. napi_disable(&priv->napi);
  475. atomic_inc(&priv->reset_pending);
  476. cpmac_hw_stop(priv->dev);
  477. if (!schedule_work(&priv->reset_work))
  478. atomic_dec(&priv->reset_pending);
  479. return 0;
  480. }
  481. static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  482. {
  483. int queue;
  484. unsigned int len;
  485. struct cpmac_desc *desc;
  486. struct cpmac_priv *priv = netdev_priv(dev);
  487. if (unlikely(atomic_read(&priv->reset_pending)))
  488. return NETDEV_TX_BUSY;
  489. if (unlikely(skb_padto(skb, ETH_ZLEN)))
  490. return NETDEV_TX_OK;
  491. len = max_t(unsigned int, skb->len, ETH_ZLEN);
  492. queue = skb_get_queue_mapping(skb);
  493. netif_stop_subqueue(dev, queue);
  494. desc = &priv->desc_ring[queue];
  495. if (unlikely(desc->dataflags & CPMAC_OWN)) {
  496. if (netif_msg_tx_err(priv) && net_ratelimit())
  497. netdev_warn(dev, "tx dma ring full\n");
  498. return NETDEV_TX_BUSY;
  499. }
  500. spin_lock(&priv->lock);
  501. spin_unlock(&priv->lock);
  502. desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
  503. desc->skb = skb;
  504. desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
  505. DMA_TO_DEVICE);
  506. desc->hw_data = (u32)desc->data_mapping;
  507. desc->datalen = len;
  508. desc->buflen = len;
  509. if (unlikely(netif_msg_tx_queued(priv)))
  510. netdev_dbg(dev, "sending 0x%p, len=%d\n", skb, skb->len);
  511. if (unlikely(netif_msg_hw(priv)))
  512. cpmac_dump_desc(dev, desc);
  513. if (unlikely(netif_msg_pktdata(priv)))
  514. cpmac_dump_skb(dev, skb);
  515. cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
  516. return NETDEV_TX_OK;
  517. }
  518. static void cpmac_end_xmit(struct net_device *dev, int queue)
  519. {
  520. struct cpmac_desc *desc;
  521. struct cpmac_priv *priv = netdev_priv(dev);
  522. desc = &priv->desc_ring[queue];
  523. cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
  524. if (likely(desc->skb)) {
  525. spin_lock(&priv->lock);
  526. dev->stats.tx_packets++;
  527. dev->stats.tx_bytes += desc->skb->len;
  528. spin_unlock(&priv->lock);
  529. dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
  530. DMA_TO_DEVICE);
  531. if (unlikely(netif_msg_tx_done(priv)))
  532. netdev_dbg(dev, "sent 0x%p, len=%d\n",
  533. desc->skb, desc->skb->len);
  534. dev_kfree_skb_irq(desc->skb);
  535. desc->skb = NULL;
  536. if (__netif_subqueue_stopped(dev, queue))
  537. netif_wake_subqueue(dev, queue);
  538. } else {
  539. if (netif_msg_tx_err(priv) && net_ratelimit())
  540. netdev_warn(dev, "end_xmit: spurious interrupt\n");
  541. if (__netif_subqueue_stopped(dev, queue))
  542. netif_wake_subqueue(dev, queue);
  543. }
  544. }
  545. static void cpmac_hw_stop(struct net_device *dev)
  546. {
  547. int i;
  548. struct cpmac_priv *priv = netdev_priv(dev);
  549. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  550. ar7_device_reset(pdata->reset_bit);
  551. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  552. cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
  553. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  554. cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
  555. for (i = 0; i < 8; i++) {
  556. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  557. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  558. }
  559. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  560. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  561. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  562. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  563. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  564. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
  565. }
  566. static void cpmac_hw_start(struct net_device *dev)
  567. {
  568. int i;
  569. struct cpmac_priv *priv = netdev_priv(dev);
  570. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  571. ar7_device_reset(pdata->reset_bit);
  572. for (i = 0; i < 8; i++) {
  573. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  574. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  575. }
  576. cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
  577. cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
  578. MBP_RXMCAST);
  579. cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
  580. for (i = 0; i < 8; i++)
  581. cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
  582. cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
  583. cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
  584. (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
  585. (dev->dev_addr[3] << 24));
  586. cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
  587. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  588. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  589. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  590. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  591. cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
  592. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  593. cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
  594. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  595. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  596. cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
  597. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  598. cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
  599. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  600. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
  601. MAC_FDX);
  602. }
  603. static void cpmac_clear_rx(struct net_device *dev)
  604. {
  605. struct cpmac_priv *priv = netdev_priv(dev);
  606. struct cpmac_desc *desc;
  607. int i;
  608. if (unlikely(!priv->rx_head))
  609. return;
  610. desc = priv->rx_head;
  611. for (i = 0; i < priv->ring_size; i++) {
  612. if ((desc->dataflags & CPMAC_OWN) == 0) {
  613. if (netif_msg_rx_err(priv) && net_ratelimit())
  614. netdev_warn(dev, "packet dropped\n");
  615. if (unlikely(netif_msg_hw(priv)))
  616. cpmac_dump_desc(dev, desc);
  617. desc->dataflags = CPMAC_OWN;
  618. dev->stats.rx_dropped++;
  619. }
  620. desc->hw_next = desc->next->mapping;
  621. desc = desc->next;
  622. }
  623. priv->rx_head->prev->hw_next = 0;
  624. }
  625. static void cpmac_clear_tx(struct net_device *dev)
  626. {
  627. struct cpmac_priv *priv = netdev_priv(dev);
  628. int i;
  629. if (unlikely(!priv->desc_ring))
  630. return;
  631. for (i = 0; i < CPMAC_QUEUES; i++) {
  632. priv->desc_ring[i].dataflags = 0;
  633. if (priv->desc_ring[i].skb) {
  634. dev_kfree_skb_any(priv->desc_ring[i].skb);
  635. priv->desc_ring[i].skb = NULL;
  636. }
  637. }
  638. }
  639. static void cpmac_hw_error(struct work_struct *work)
  640. {
  641. struct cpmac_priv *priv =
  642. container_of(work, struct cpmac_priv, reset_work);
  643. spin_lock(&priv->rx_lock);
  644. cpmac_clear_rx(priv->dev);
  645. spin_unlock(&priv->rx_lock);
  646. cpmac_clear_tx(priv->dev);
  647. cpmac_hw_start(priv->dev);
  648. barrier();
  649. atomic_dec(&priv->reset_pending);
  650. netif_tx_wake_all_queues(priv->dev);
  651. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  652. }
  653. static void cpmac_check_status(struct net_device *dev)
  654. {
  655. struct cpmac_priv *priv = netdev_priv(dev);
  656. u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
  657. int rx_channel = (macstatus >> 8) & 7;
  658. int rx_code = (macstatus >> 12) & 15;
  659. int tx_channel = (macstatus >> 16) & 7;
  660. int tx_code = (macstatus >> 20) & 15;
  661. if (rx_code || tx_code) {
  662. if (netif_msg_drv(priv) && net_ratelimit()) {
  663. /* Can't find any documentation on what these
  664. * error codes actually are. So just log them and hope..
  665. */
  666. if (rx_code)
  667. netdev_warn(dev, "host error %d on rx "
  668. "channel %d (macstatus %08x), resetting\n",
  669. rx_code, rx_channel, macstatus);
  670. if (tx_code)
  671. netdev_warn(dev, "host error %d on tx "
  672. "channel %d (macstatus %08x), resetting\n",
  673. tx_code, tx_channel, macstatus);
  674. }
  675. netif_tx_stop_all_queues(dev);
  676. cpmac_hw_stop(dev);
  677. if (schedule_work(&priv->reset_work))
  678. atomic_inc(&priv->reset_pending);
  679. if (unlikely(netif_msg_hw(priv)))
  680. cpmac_dump_regs(dev);
  681. }
  682. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  683. }
  684. static irqreturn_t cpmac_irq(int irq, void *dev_id)
  685. {
  686. struct net_device *dev = dev_id;
  687. struct cpmac_priv *priv;
  688. int queue;
  689. u32 status;
  690. priv = netdev_priv(dev);
  691. status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
  692. if (unlikely(netif_msg_intr(priv)))
  693. netdev_dbg(dev, "interrupt status: 0x%08x\n", status);
  694. if (status & MAC_INT_TX)
  695. cpmac_end_xmit(dev, (status & 7));
  696. if (status & MAC_INT_RX) {
  697. queue = (status >> 8) & 7;
  698. if (napi_schedule_prep(&priv->napi)) {
  699. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
  700. __napi_schedule(&priv->napi);
  701. }
  702. }
  703. cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
  704. if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
  705. cpmac_check_status(dev);
  706. return IRQ_HANDLED;
  707. }
  708. static void cpmac_tx_timeout(struct net_device *dev)
  709. {
  710. struct cpmac_priv *priv = netdev_priv(dev);
  711. spin_lock(&priv->lock);
  712. dev->stats.tx_errors++;
  713. spin_unlock(&priv->lock);
  714. if (netif_msg_tx_err(priv) && net_ratelimit())
  715. netdev_warn(dev, "transmit timeout\n");
  716. atomic_inc(&priv->reset_pending);
  717. barrier();
  718. cpmac_clear_tx(dev);
  719. barrier();
  720. atomic_dec(&priv->reset_pending);
  721. netif_tx_wake_all_queues(priv->dev);
  722. }
  723. static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  724. {
  725. if (!(netif_running(dev)))
  726. return -EINVAL;
  727. if (!dev->phydev)
  728. return -EINVAL;
  729. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  730. }
  731. static void cpmac_get_ringparam(struct net_device *dev,
  732. struct ethtool_ringparam *ring)
  733. {
  734. struct cpmac_priv *priv = netdev_priv(dev);
  735. ring->rx_max_pending = 1024;
  736. ring->rx_mini_max_pending = 1;
  737. ring->rx_jumbo_max_pending = 1;
  738. ring->tx_max_pending = 1;
  739. ring->rx_pending = priv->ring_size;
  740. ring->rx_mini_pending = 1;
  741. ring->rx_jumbo_pending = 1;
  742. ring->tx_pending = 1;
  743. }
  744. static int cpmac_set_ringparam(struct net_device *dev,
  745. struct ethtool_ringparam *ring)
  746. {
  747. struct cpmac_priv *priv = netdev_priv(dev);
  748. if (netif_running(dev))
  749. return -EBUSY;
  750. priv->ring_size = ring->rx_pending;
  751. return 0;
  752. }
  753. static void cpmac_get_drvinfo(struct net_device *dev,
  754. struct ethtool_drvinfo *info)
  755. {
  756. strlcpy(info->driver, "cpmac", sizeof(info->driver));
  757. strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
  758. snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
  759. }
  760. static const struct ethtool_ops cpmac_ethtool_ops = {
  761. .get_drvinfo = cpmac_get_drvinfo,
  762. .get_link = ethtool_op_get_link,
  763. .get_ringparam = cpmac_get_ringparam,
  764. .set_ringparam = cpmac_set_ringparam,
  765. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  766. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  767. };
  768. static void cpmac_adjust_link(struct net_device *dev)
  769. {
  770. struct cpmac_priv *priv = netdev_priv(dev);
  771. int new_state = 0;
  772. spin_lock(&priv->lock);
  773. if (dev->phydev->link) {
  774. netif_tx_start_all_queues(dev);
  775. if (dev->phydev->duplex != priv->oldduplex) {
  776. new_state = 1;
  777. priv->oldduplex = dev->phydev->duplex;
  778. }
  779. if (dev->phydev->speed != priv->oldspeed) {
  780. new_state = 1;
  781. priv->oldspeed = dev->phydev->speed;
  782. }
  783. if (!priv->oldlink) {
  784. new_state = 1;
  785. priv->oldlink = 1;
  786. }
  787. } else if (priv->oldlink) {
  788. new_state = 1;
  789. priv->oldlink = 0;
  790. priv->oldspeed = 0;
  791. priv->oldduplex = -1;
  792. }
  793. if (new_state && netif_msg_link(priv) && net_ratelimit())
  794. phy_print_status(dev->phydev);
  795. spin_unlock(&priv->lock);
  796. }
  797. static int cpmac_open(struct net_device *dev)
  798. {
  799. int i, size, res;
  800. struct cpmac_priv *priv = netdev_priv(dev);
  801. struct resource *mem;
  802. struct cpmac_desc *desc;
  803. struct sk_buff *skb;
  804. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  805. if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
  806. if (netif_msg_drv(priv))
  807. netdev_err(dev, "failed to request registers\n");
  808. res = -ENXIO;
  809. goto fail_reserve;
  810. }
  811. priv->regs = ioremap(mem->start, resource_size(mem));
  812. if (!priv->regs) {
  813. if (netif_msg_drv(priv))
  814. netdev_err(dev, "failed to remap registers\n");
  815. res = -ENXIO;
  816. goto fail_remap;
  817. }
  818. size = priv->ring_size + CPMAC_QUEUES;
  819. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  820. sizeof(struct cpmac_desc) * size,
  821. &priv->dma_ring,
  822. GFP_KERNEL);
  823. if (!priv->desc_ring) {
  824. res = -ENOMEM;
  825. goto fail_alloc;
  826. }
  827. for (i = 0; i < size; i++)
  828. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  829. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  830. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  831. skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
  832. if (unlikely(!skb)) {
  833. res = -ENOMEM;
  834. goto fail_desc;
  835. }
  836. desc->skb = skb;
  837. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  838. CPMAC_SKB_SIZE,
  839. DMA_FROM_DEVICE);
  840. desc->hw_data = (u32)desc->data_mapping;
  841. desc->buflen = CPMAC_SKB_SIZE;
  842. desc->dataflags = CPMAC_OWN;
  843. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  844. desc->next->prev = desc;
  845. desc->hw_next = (u32)desc->next->mapping;
  846. }
  847. priv->rx_head->prev->hw_next = (u32)0;
  848. res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
  849. if (res) {
  850. if (netif_msg_drv(priv))
  851. netdev_err(dev, "failed to obtain irq\n");
  852. goto fail_irq;
  853. }
  854. atomic_set(&priv->reset_pending, 0);
  855. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  856. cpmac_hw_start(dev);
  857. napi_enable(&priv->napi);
  858. dev->phydev->state = PHY_CHANGELINK;
  859. phy_start(dev->phydev);
  860. return 0;
  861. fail_irq:
  862. fail_desc:
  863. for (i = 0; i < priv->ring_size; i++) {
  864. if (priv->rx_head[i].skb) {
  865. dma_unmap_single(&dev->dev,
  866. priv->rx_head[i].data_mapping,
  867. CPMAC_SKB_SIZE,
  868. DMA_FROM_DEVICE);
  869. kfree_skb(priv->rx_head[i].skb);
  870. }
  871. }
  872. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) * size,
  873. priv->desc_ring, priv->dma_ring);
  874. fail_alloc:
  875. iounmap(priv->regs);
  876. fail_remap:
  877. release_mem_region(mem->start, resource_size(mem));
  878. fail_reserve:
  879. return res;
  880. }
  881. static int cpmac_stop(struct net_device *dev)
  882. {
  883. int i;
  884. struct cpmac_priv *priv = netdev_priv(dev);
  885. struct resource *mem;
  886. netif_tx_stop_all_queues(dev);
  887. cancel_work_sync(&priv->reset_work);
  888. napi_disable(&priv->napi);
  889. phy_stop(dev->phydev);
  890. cpmac_hw_stop(dev);
  891. for (i = 0; i < 8; i++)
  892. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  893. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  894. cpmac_write(priv->regs, CPMAC_MBP, 0);
  895. free_irq(dev->irq, dev);
  896. iounmap(priv->regs);
  897. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  898. release_mem_region(mem->start, resource_size(mem));
  899. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  900. for (i = 0; i < priv->ring_size; i++) {
  901. if (priv->rx_head[i].skb) {
  902. dma_unmap_single(&dev->dev,
  903. priv->rx_head[i].data_mapping,
  904. CPMAC_SKB_SIZE,
  905. DMA_FROM_DEVICE);
  906. kfree_skb(priv->rx_head[i].skb);
  907. }
  908. }
  909. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  910. (CPMAC_QUEUES + priv->ring_size),
  911. priv->desc_ring, priv->dma_ring);
  912. return 0;
  913. }
  914. static const struct net_device_ops cpmac_netdev_ops = {
  915. .ndo_open = cpmac_open,
  916. .ndo_stop = cpmac_stop,
  917. .ndo_start_xmit = cpmac_start_xmit,
  918. .ndo_tx_timeout = cpmac_tx_timeout,
  919. .ndo_set_rx_mode = cpmac_set_multicast_list,
  920. .ndo_do_ioctl = cpmac_ioctl,
  921. .ndo_validate_addr = eth_validate_addr,
  922. .ndo_set_mac_address = eth_mac_addr,
  923. };
  924. static int external_switch;
  925. static int cpmac_probe(struct platform_device *pdev)
  926. {
  927. int rc, phy_id;
  928. char mdio_bus_id[MII_BUS_ID_SIZE];
  929. struct resource *mem;
  930. struct cpmac_priv *priv;
  931. struct net_device *dev;
  932. struct plat_cpmac_data *pdata;
  933. struct phy_device *phydev = NULL;
  934. pdata = dev_get_platdata(&pdev->dev);
  935. if (external_switch || dumb_switch) {
  936. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  937. phy_id = pdev->id;
  938. } else {
  939. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  940. if (!(pdata->phy_mask & (1 << phy_id)))
  941. continue;
  942. if (!mdiobus_get_phy(cpmac_mii, phy_id))
  943. continue;
  944. strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
  945. break;
  946. }
  947. }
  948. if (phy_id == PHY_MAX_ADDR) {
  949. dev_err(&pdev->dev, "no PHY present, falling back "
  950. "to switch on MDIO bus 0\n");
  951. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  952. phy_id = pdev->id;
  953. }
  954. mdio_bus_id[sizeof(mdio_bus_id) - 1] = '\0';
  955. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  956. if (!dev)
  957. return -ENOMEM;
  958. SET_NETDEV_DEV(dev, &pdev->dev);
  959. platform_set_drvdata(pdev, dev);
  960. priv = netdev_priv(dev);
  961. priv->pdev = pdev;
  962. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  963. if (!mem) {
  964. rc = -ENODEV;
  965. goto fail;
  966. }
  967. dev->irq = platform_get_irq_byname(pdev, "irq");
  968. dev->netdev_ops = &cpmac_netdev_ops;
  969. dev->ethtool_ops = &cpmac_ethtool_ops;
  970. netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
  971. spin_lock_init(&priv->lock);
  972. spin_lock_init(&priv->rx_lock);
  973. priv->dev = dev;
  974. priv->ring_size = 64;
  975. priv->msg_enable = netif_msg_init(debug_level, 0xff);
  976. memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
  977. snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
  978. mdio_bus_id, phy_id);
  979. phydev = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
  980. PHY_INTERFACE_MODE_MII);
  981. if (IS_ERR(phydev)) {
  982. if (netif_msg_drv(priv))
  983. dev_err(&pdev->dev, "Could not attach to PHY\n");
  984. rc = PTR_ERR(phydev);
  985. goto fail;
  986. }
  987. rc = register_netdev(dev);
  988. if (rc) {
  989. dev_err(&pdev->dev, "Could not register net device\n");
  990. goto fail;
  991. }
  992. if (netif_msg_probe(priv)) {
  993. dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
  994. "mac: %pM\n", (void *)mem->start, dev->irq,
  995. priv->phy_name, dev->dev_addr);
  996. }
  997. return 0;
  998. fail:
  999. free_netdev(dev);
  1000. return rc;
  1001. }
  1002. static int cpmac_remove(struct platform_device *pdev)
  1003. {
  1004. struct net_device *dev = platform_get_drvdata(pdev);
  1005. unregister_netdev(dev);
  1006. free_netdev(dev);
  1007. return 0;
  1008. }
  1009. static struct platform_driver cpmac_driver = {
  1010. .driver = {
  1011. .name = "cpmac",
  1012. },
  1013. .probe = cpmac_probe,
  1014. .remove = cpmac_remove,
  1015. };
  1016. int cpmac_init(void)
  1017. {
  1018. u32 mask;
  1019. int i, res;
  1020. cpmac_mii = mdiobus_alloc();
  1021. if (cpmac_mii == NULL)
  1022. return -ENOMEM;
  1023. cpmac_mii->name = "cpmac-mii";
  1024. cpmac_mii->read = cpmac_mdio_read;
  1025. cpmac_mii->write = cpmac_mdio_write;
  1026. cpmac_mii->reset = cpmac_mdio_reset;
  1027. cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
  1028. if (!cpmac_mii->priv) {
  1029. pr_err("Can't ioremap mdio registers\n");
  1030. res = -ENXIO;
  1031. goto fail_alloc;
  1032. }
  1033. /* FIXME: unhardcode gpio&reset bits */
  1034. ar7_gpio_disable(26);
  1035. ar7_gpio_disable(27);
  1036. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  1037. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  1038. ar7_device_reset(AR7_RESET_BIT_EPHY);
  1039. cpmac_mii->reset(cpmac_mii);
  1040. for (i = 0; i < 300; i++) {
  1041. mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
  1042. if (mask)
  1043. break;
  1044. else
  1045. msleep(10);
  1046. }
  1047. mask &= 0x7fffffff;
  1048. if (mask & (mask - 1)) {
  1049. external_switch = 1;
  1050. mask = 0;
  1051. }
  1052. cpmac_mii->phy_mask = ~(mask | 0x80000000);
  1053. snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
  1054. res = mdiobus_register(cpmac_mii);
  1055. if (res)
  1056. goto fail_mii;
  1057. res = platform_driver_register(&cpmac_driver);
  1058. if (res)
  1059. goto fail_cpmac;
  1060. return 0;
  1061. fail_cpmac:
  1062. mdiobus_unregister(cpmac_mii);
  1063. fail_mii:
  1064. iounmap(cpmac_mii->priv);
  1065. fail_alloc:
  1066. mdiobus_free(cpmac_mii);
  1067. return res;
  1068. }
  1069. void cpmac_exit(void)
  1070. {
  1071. platform_driver_unregister(&cpmac_driver);
  1072. mdiobus_unregister(cpmac_mii);
  1073. iounmap(cpmac_mii->priv);
  1074. mdiobus_free(cpmac_mii);
  1075. }
  1076. module_init(cpmac_init);
  1077. module_exit(cpmac_exit);