cpmac.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
  41. MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
  42. MODULE_LICENSE("GPL");
  43. MODULE_ALIAS("platform:cpmac");
  44. static int debug_level = 8;
  45. static int dumb_switch;
  46. /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
  47. module_param(debug_level, int, 0444);
  48. module_param(dumb_switch, int, 0444);
  49. MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
  50. MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
  51. #define CPMAC_VERSION "0.5.2"
  52. /* frame size + 802.1q tag + FCS size */
  53. #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
  54. #define CPMAC_QUEUES 8
  55. /* Ethernet registers */
  56. #define CPMAC_TX_CONTROL 0x0004
  57. #define CPMAC_TX_TEARDOWN 0x0008
  58. #define CPMAC_RX_CONTROL 0x0014
  59. #define CPMAC_RX_TEARDOWN 0x0018
  60. #define CPMAC_MBP 0x0100
  61. #define MBP_RXPASSCRC 0x40000000
  62. #define MBP_RXQOS 0x20000000
  63. #define MBP_RXNOCHAIN 0x10000000
  64. #define MBP_RXCMF 0x01000000
  65. #define MBP_RXSHORT 0x00800000
  66. #define MBP_RXCEF 0x00400000
  67. #define MBP_RXPROMISC 0x00200000
  68. #define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
  69. #define MBP_RXBCAST 0x00002000
  70. #define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
  71. #define MBP_RXMCAST 0x00000020
  72. #define MBP_MCASTCHAN(channel) ((channel) & 0x7)
  73. #define CPMAC_UNICAST_ENABLE 0x0104
  74. #define CPMAC_UNICAST_CLEAR 0x0108
  75. #define CPMAC_MAX_LENGTH 0x010c
  76. #define CPMAC_BUFFER_OFFSET 0x0110
  77. #define CPMAC_MAC_CONTROL 0x0160
  78. #define MAC_TXPTYPE 0x00000200
  79. #define MAC_TXPACE 0x00000040
  80. #define MAC_MII 0x00000020
  81. #define MAC_TXFLOW 0x00000010
  82. #define MAC_RXFLOW 0x00000008
  83. #define MAC_MTEST 0x00000004
  84. #define MAC_LOOPBACK 0x00000002
  85. #define MAC_FDX 0x00000001
  86. #define CPMAC_MAC_STATUS 0x0164
  87. #define MAC_STATUS_QOS 0x00000004
  88. #define MAC_STATUS_RXFLOW 0x00000002
  89. #define MAC_STATUS_TXFLOW 0x00000001
  90. #define CPMAC_TX_INT_ENABLE 0x0178
  91. #define CPMAC_TX_INT_CLEAR 0x017c
  92. #define CPMAC_MAC_INT_VECTOR 0x0180
  93. #define MAC_INT_STATUS 0x00080000
  94. #define MAC_INT_HOST 0x00040000
  95. #define MAC_INT_RX 0x00020000
  96. #define MAC_INT_TX 0x00010000
  97. #define CPMAC_MAC_EOI_VECTOR 0x0184
  98. #define CPMAC_RX_INT_ENABLE 0x0198
  99. #define CPMAC_RX_INT_CLEAR 0x019c
  100. #define CPMAC_MAC_INT_ENABLE 0x01a8
  101. #define CPMAC_MAC_INT_CLEAR 0x01ac
  102. #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
  103. #define CPMAC_MAC_ADDR_MID 0x01d0
  104. #define CPMAC_MAC_ADDR_HI 0x01d4
  105. #define CPMAC_MAC_HASH_LO 0x01d8
  106. #define CPMAC_MAC_HASH_HI 0x01dc
  107. #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
  108. #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
  109. #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
  110. #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
  111. #define CPMAC_REG_END 0x0680
  112. /* Rx/Tx statistics
  113. * TODO: use some of them to fill stats in cpmac_stats()
  114. */
  115. #define CPMAC_STATS_RX_GOOD 0x0200
  116. #define CPMAC_STATS_RX_BCAST 0x0204
  117. #define CPMAC_STATS_RX_MCAST 0x0208
  118. #define CPMAC_STATS_RX_PAUSE 0x020c
  119. #define CPMAC_STATS_RX_CRC 0x0210
  120. #define CPMAC_STATS_RX_ALIGN 0x0214
  121. #define CPMAC_STATS_RX_OVER 0x0218
  122. #define CPMAC_STATS_RX_JABBER 0x021c
  123. #define CPMAC_STATS_RX_UNDER 0x0220
  124. #define CPMAC_STATS_RX_FRAG 0x0224
  125. #define CPMAC_STATS_RX_FILTER 0x0228
  126. #define CPMAC_STATS_RX_QOSFILTER 0x022c
  127. #define CPMAC_STATS_RX_OCTETS 0x0230
  128. #define CPMAC_STATS_TX_GOOD 0x0234
  129. #define CPMAC_STATS_TX_BCAST 0x0238
  130. #define CPMAC_STATS_TX_MCAST 0x023c
  131. #define CPMAC_STATS_TX_PAUSE 0x0240
  132. #define CPMAC_STATS_TX_DEFER 0x0244
  133. #define CPMAC_STATS_TX_COLLISION 0x0248
  134. #define CPMAC_STATS_TX_SINGLECOLL 0x024c
  135. #define CPMAC_STATS_TX_MULTICOLL 0x0250
  136. #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
  137. #define CPMAC_STATS_TX_LATECOLL 0x0258
  138. #define CPMAC_STATS_TX_UNDERRUN 0x025c
  139. #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
  140. #define CPMAC_STATS_TX_OCTETS 0x0264
  141. #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
  142. #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
  143. (reg)))
  144. /* MDIO bus */
  145. #define CPMAC_MDIO_VERSION 0x0000
  146. #define CPMAC_MDIO_CONTROL 0x0004
  147. #define MDIOC_IDLE 0x80000000
  148. #define MDIOC_ENABLE 0x40000000
  149. #define MDIOC_PREAMBLE 0x00100000
  150. #define MDIOC_FAULT 0x00080000
  151. #define MDIOC_FAULTDETECT 0x00040000
  152. #define MDIOC_INTTEST 0x00020000
  153. #define MDIOC_CLKDIV(div) ((div) & 0xff)
  154. #define CPMAC_MDIO_ALIVE 0x0008
  155. #define CPMAC_MDIO_LINK 0x000c
  156. #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
  157. #define MDIO_BUSY 0x80000000
  158. #define MDIO_WRITE 0x40000000
  159. #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
  160. #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
  161. #define MDIO_DATA(data) ((data) & 0xffff)
  162. #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
  163. #define PHYSEL_LINKSEL 0x00000040
  164. #define PHYSEL_LINKINT 0x00000020
  165. struct cpmac_desc {
  166. u32 hw_next;
  167. u32 hw_data;
  168. u16 buflen;
  169. u16 bufflags;
  170. u16 datalen;
  171. u16 dataflags;
  172. #define CPMAC_SOP 0x8000
  173. #define CPMAC_EOP 0x4000
  174. #define CPMAC_OWN 0x2000
  175. #define CPMAC_EOQ 0x1000
  176. struct sk_buff *skb;
  177. struct cpmac_desc *next;
  178. struct cpmac_desc *prev;
  179. dma_addr_t mapping;
  180. dma_addr_t data_mapping;
  181. };
  182. struct cpmac_priv {
  183. spinlock_t lock;
  184. spinlock_t rx_lock;
  185. struct cpmac_desc *rx_head;
  186. int ring_size;
  187. struct cpmac_desc *desc_ring;
  188. dma_addr_t dma_ring;
  189. void __iomem *regs;
  190. struct mii_bus *mii_bus;
  191. struct phy_device *phy;
  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 int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
  285. static struct mii_bus *cpmac_mii;
  286. static void cpmac_set_multicast_list(struct net_device *dev)
  287. {
  288. struct netdev_hw_addr *ha;
  289. u8 tmp;
  290. u32 mbp, bit, hash[2] = { 0, };
  291. struct cpmac_priv *priv = netdev_priv(dev);
  292. mbp = cpmac_read(priv->regs, CPMAC_MBP);
  293. if (dev->flags & IFF_PROMISC) {
  294. cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
  295. MBP_RXPROMISC);
  296. } else {
  297. cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
  298. if (dev->flags & IFF_ALLMULTI) {
  299. /* enable all multicast mode */
  300. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
  301. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
  302. } else {
  303. /* cpmac uses some strange mac address hashing
  304. * (not crc32)
  305. */
  306. netdev_for_each_mc_addr(ha, dev) {
  307. bit = 0;
  308. tmp = ha->addr[0];
  309. bit ^= (tmp >> 2) ^ (tmp << 4);
  310. tmp = ha->addr[1];
  311. bit ^= (tmp >> 4) ^ (tmp << 2);
  312. tmp = ha->addr[2];
  313. bit ^= (tmp >> 6) ^ tmp;
  314. tmp = ha->addr[3];
  315. bit ^= (tmp >> 2) ^ (tmp << 4);
  316. tmp = ha->addr[4];
  317. bit ^= (tmp >> 4) ^ (tmp << 2);
  318. tmp = ha->addr[5];
  319. bit ^= (tmp >> 6) ^ tmp;
  320. bit &= 0x3f;
  321. hash[bit / 32] |= 1 << (bit % 32);
  322. }
  323. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
  324. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
  325. }
  326. }
  327. }
  328. static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
  329. struct cpmac_desc *desc)
  330. {
  331. struct sk_buff *skb, *result = NULL;
  332. if (unlikely(netif_msg_hw(priv)))
  333. cpmac_dump_desc(priv->dev, desc);
  334. cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
  335. if (unlikely(!desc->datalen)) {
  336. if (netif_msg_rx_err(priv) && net_ratelimit())
  337. netdev_warn(priv->dev, "rx: spurious interrupt\n");
  338. return NULL;
  339. }
  340. skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
  341. if (likely(skb)) {
  342. skb_put(desc->skb, desc->datalen);
  343. desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
  344. skb_checksum_none_assert(desc->skb);
  345. priv->dev->stats.rx_packets++;
  346. priv->dev->stats.rx_bytes += desc->datalen;
  347. result = desc->skb;
  348. dma_unmap_single(&priv->dev->dev, desc->data_mapping,
  349. CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
  350. desc->skb = skb;
  351. desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
  352. CPMAC_SKB_SIZE,
  353. DMA_FROM_DEVICE);
  354. desc->hw_data = (u32)desc->data_mapping;
  355. if (unlikely(netif_msg_pktdata(priv))) {
  356. netdev_dbg(priv->dev, "received packet:\n");
  357. cpmac_dump_skb(priv->dev, result);
  358. }
  359. } else {
  360. if (netif_msg_rx_err(priv) && net_ratelimit())
  361. netdev_warn(priv->dev,
  362. "low on skbs, dropping packet\n");
  363. priv->dev->stats.rx_dropped++;
  364. }
  365. desc->buflen = CPMAC_SKB_SIZE;
  366. desc->dataflags = CPMAC_OWN;
  367. return result;
  368. }
  369. static int cpmac_poll(struct napi_struct *napi, int budget)
  370. {
  371. struct sk_buff *skb;
  372. struct cpmac_desc *desc, *restart;
  373. struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
  374. int received = 0, processed = 0;
  375. spin_lock(&priv->rx_lock);
  376. if (unlikely(!priv->rx_head)) {
  377. if (netif_msg_rx_err(priv) && net_ratelimit())
  378. netdev_warn(priv->dev, "rx: polling, but no queue\n");
  379. spin_unlock(&priv->rx_lock);
  380. napi_complete(napi);
  381. return 0;
  382. }
  383. desc = priv->rx_head;
  384. restart = NULL;
  385. while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
  386. processed++;
  387. if ((desc->dataflags & CPMAC_EOQ) != 0) {
  388. /* The last update to eoq->hw_next didn't happen
  389. * soon enough, and the receiver stopped here.
  390. * Remember this descriptor so we can restart
  391. * the receiver after freeing some space.
  392. */
  393. if (unlikely(restart)) {
  394. if (netif_msg_rx_err(priv))
  395. netdev_err(priv->dev, "poll found a"
  396. " duplicate EOQ: %p and %p\n",
  397. restart, desc);
  398. goto fatal_error;
  399. }
  400. restart = desc->next;
  401. }
  402. skb = cpmac_rx_one(priv, desc);
  403. if (likely(skb)) {
  404. netif_receive_skb(skb);
  405. received++;
  406. }
  407. desc = desc->next;
  408. }
  409. if (desc != priv->rx_head) {
  410. /* We freed some buffers, but not the whole ring,
  411. * add what we did free to the rx list
  412. */
  413. desc->prev->hw_next = (u32)0;
  414. priv->rx_head->prev->hw_next = priv->rx_head->mapping;
  415. }
  416. /* Optimization: If we did not actually process an EOQ (perhaps because
  417. * of quota limits), check to see if the tail of the queue has EOQ set.
  418. * We should immediately restart in that case so that the receiver can
  419. * restart and run in parallel with more packet processing.
  420. * This lets us handle slightly larger bursts before running
  421. * out of ring space (assuming dev->weight < ring_size)
  422. */
  423. if (!restart &&
  424. (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
  425. == CPMAC_EOQ &&
  426. (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
  427. /* reset EOQ so the poll loop (above) doesn't try to
  428. * restart this when it eventually gets to this descriptor.
  429. */
  430. priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
  431. restart = priv->rx_head;
  432. }
  433. if (restart) {
  434. priv->dev->stats.rx_errors++;
  435. priv->dev->stats.rx_fifo_errors++;
  436. if (netif_msg_rx_err(priv) && net_ratelimit())
  437. netdev_warn(priv->dev, "rx dma ring overrun\n");
  438. if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
  439. if (netif_msg_drv(priv))
  440. netdev_err(priv->dev, "cpmac_poll is trying "
  441. "to restart rx from a descriptor "
  442. "that's not free: %p\n", restart);
  443. goto fatal_error;
  444. }
  445. cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
  446. }
  447. priv->rx_head = desc;
  448. spin_unlock(&priv->rx_lock);
  449. if (unlikely(netif_msg_rx_status(priv)))
  450. netdev_dbg(priv->dev, "poll processed %d packets\n", received);
  451. if (processed == 0) {
  452. /* we ran out of packets to read,
  453. * revert to interrupt-driven mode
  454. */
  455. napi_complete(napi);
  456. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  457. return 0;
  458. }
  459. return 1;
  460. fatal_error:
  461. /* Something went horribly wrong.
  462. * Reset hardware to try to recover rather than wedging.
  463. */
  464. if (netif_msg_drv(priv)) {
  465. netdev_err(priv->dev, "cpmac_poll is confused. "
  466. "Resetting hardware\n");
  467. cpmac_dump_all_desc(priv->dev);
  468. netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
  469. cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
  470. cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
  471. }
  472. spin_unlock(&priv->rx_lock);
  473. napi_complete(napi);
  474. netif_tx_stop_all_queues(priv->dev);
  475. napi_disable(&priv->napi);
  476. atomic_inc(&priv->reset_pending);
  477. cpmac_hw_stop(priv->dev);
  478. if (!schedule_work(&priv->reset_work))
  479. atomic_dec(&priv->reset_pending);
  480. return 0;
  481. }
  482. static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  483. {
  484. int queue, 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(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. struct cpmac_priv *priv = netdev_priv(dev);
  726. if (!(netif_running(dev)))
  727. return -EINVAL;
  728. if (!priv->phy)
  729. return -EINVAL;
  730. return phy_mii_ioctl(priv->phy, ifr, cmd);
  731. }
  732. static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  733. {
  734. struct cpmac_priv *priv = netdev_priv(dev);
  735. if (priv->phy)
  736. return phy_ethtool_gset(priv->phy, cmd);
  737. return -EINVAL;
  738. }
  739. static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  740. {
  741. struct cpmac_priv *priv = netdev_priv(dev);
  742. if (!capable(CAP_NET_ADMIN))
  743. return -EPERM;
  744. if (priv->phy)
  745. return phy_ethtool_sset(priv->phy, cmd);
  746. return -EINVAL;
  747. }
  748. static void cpmac_get_ringparam(struct net_device *dev,
  749. struct ethtool_ringparam *ring)
  750. {
  751. struct cpmac_priv *priv = netdev_priv(dev);
  752. ring->rx_max_pending = 1024;
  753. ring->rx_mini_max_pending = 1;
  754. ring->rx_jumbo_max_pending = 1;
  755. ring->tx_max_pending = 1;
  756. ring->rx_pending = priv->ring_size;
  757. ring->rx_mini_pending = 1;
  758. ring->rx_jumbo_pending = 1;
  759. ring->tx_pending = 1;
  760. }
  761. static int cpmac_set_ringparam(struct net_device *dev,
  762. struct ethtool_ringparam *ring)
  763. {
  764. struct cpmac_priv *priv = netdev_priv(dev);
  765. if (netif_running(dev))
  766. return -EBUSY;
  767. priv->ring_size = ring->rx_pending;
  768. return 0;
  769. }
  770. static void cpmac_get_drvinfo(struct net_device *dev,
  771. struct ethtool_drvinfo *info)
  772. {
  773. strlcpy(info->driver, "cpmac", sizeof(info->driver));
  774. strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
  775. snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
  776. info->regdump_len = 0;
  777. }
  778. static const struct ethtool_ops cpmac_ethtool_ops = {
  779. .get_settings = cpmac_get_settings,
  780. .set_settings = cpmac_set_settings,
  781. .get_drvinfo = cpmac_get_drvinfo,
  782. .get_link = ethtool_op_get_link,
  783. .get_ringparam = cpmac_get_ringparam,
  784. .set_ringparam = cpmac_set_ringparam,
  785. };
  786. static void cpmac_adjust_link(struct net_device *dev)
  787. {
  788. struct cpmac_priv *priv = netdev_priv(dev);
  789. int new_state = 0;
  790. spin_lock(&priv->lock);
  791. if (priv->phy->link) {
  792. netif_tx_start_all_queues(dev);
  793. if (priv->phy->duplex != priv->oldduplex) {
  794. new_state = 1;
  795. priv->oldduplex = priv->phy->duplex;
  796. }
  797. if (priv->phy->speed != priv->oldspeed) {
  798. new_state = 1;
  799. priv->oldspeed = priv->phy->speed;
  800. }
  801. if (!priv->oldlink) {
  802. new_state = 1;
  803. priv->oldlink = 1;
  804. }
  805. } else if (priv->oldlink) {
  806. new_state = 1;
  807. priv->oldlink = 0;
  808. priv->oldspeed = 0;
  809. priv->oldduplex = -1;
  810. }
  811. if (new_state && netif_msg_link(priv) && net_ratelimit())
  812. phy_print_status(priv->phy);
  813. spin_unlock(&priv->lock);
  814. }
  815. static int cpmac_open(struct net_device *dev)
  816. {
  817. int i, size, res;
  818. struct cpmac_priv *priv = netdev_priv(dev);
  819. struct resource *mem;
  820. struct cpmac_desc *desc;
  821. struct sk_buff *skb;
  822. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  823. if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
  824. if (netif_msg_drv(priv))
  825. netdev_err(dev, "failed to request registers\n");
  826. res = -ENXIO;
  827. goto fail_reserve;
  828. }
  829. priv->regs = ioremap(mem->start, resource_size(mem));
  830. if (!priv->regs) {
  831. if (netif_msg_drv(priv))
  832. netdev_err(dev, "failed to remap registers\n");
  833. res = -ENXIO;
  834. goto fail_remap;
  835. }
  836. size = priv->ring_size + CPMAC_QUEUES;
  837. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  838. sizeof(struct cpmac_desc) * size,
  839. &priv->dma_ring,
  840. GFP_KERNEL);
  841. if (!priv->desc_ring) {
  842. res = -ENOMEM;
  843. goto fail_alloc;
  844. }
  845. for (i = 0; i < size; i++)
  846. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  847. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  848. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  849. skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
  850. if (unlikely(!skb)) {
  851. res = -ENOMEM;
  852. goto fail_desc;
  853. }
  854. desc->skb = skb;
  855. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  856. CPMAC_SKB_SIZE,
  857. DMA_FROM_DEVICE);
  858. desc->hw_data = (u32)desc->data_mapping;
  859. desc->buflen = CPMAC_SKB_SIZE;
  860. desc->dataflags = CPMAC_OWN;
  861. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  862. desc->next->prev = desc;
  863. desc->hw_next = (u32)desc->next->mapping;
  864. }
  865. priv->rx_head->prev->hw_next = (u32)0;
  866. res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
  867. if (res) {
  868. if (netif_msg_drv(priv))
  869. netdev_err(dev, "failed to obtain irq\n");
  870. goto fail_irq;
  871. }
  872. atomic_set(&priv->reset_pending, 0);
  873. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  874. cpmac_hw_start(dev);
  875. napi_enable(&priv->napi);
  876. priv->phy->state = PHY_CHANGELINK;
  877. phy_start(priv->phy);
  878. return 0;
  879. fail_irq:
  880. fail_desc:
  881. for (i = 0; i < priv->ring_size; i++) {
  882. if (priv->rx_head[i].skb) {
  883. dma_unmap_single(&dev->dev,
  884. priv->rx_head[i].data_mapping,
  885. CPMAC_SKB_SIZE,
  886. DMA_FROM_DEVICE);
  887. kfree_skb(priv->rx_head[i].skb);
  888. }
  889. }
  890. fail_alloc:
  891. kfree(priv->desc_ring);
  892. iounmap(priv->regs);
  893. fail_remap:
  894. release_mem_region(mem->start, resource_size(mem));
  895. fail_reserve:
  896. return res;
  897. }
  898. static int cpmac_stop(struct net_device *dev)
  899. {
  900. int i;
  901. struct cpmac_priv *priv = netdev_priv(dev);
  902. struct resource *mem;
  903. netif_tx_stop_all_queues(dev);
  904. cancel_work_sync(&priv->reset_work);
  905. napi_disable(&priv->napi);
  906. phy_stop(priv->phy);
  907. cpmac_hw_stop(dev);
  908. for (i = 0; i < 8; i++)
  909. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  910. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  911. cpmac_write(priv->regs, CPMAC_MBP, 0);
  912. free_irq(dev->irq, dev);
  913. iounmap(priv->regs);
  914. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  915. release_mem_region(mem->start, resource_size(mem));
  916. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  917. for (i = 0; i < priv->ring_size; i++) {
  918. if (priv->rx_head[i].skb) {
  919. dma_unmap_single(&dev->dev,
  920. priv->rx_head[i].data_mapping,
  921. CPMAC_SKB_SIZE,
  922. DMA_FROM_DEVICE);
  923. kfree_skb(priv->rx_head[i].skb);
  924. }
  925. }
  926. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  927. (CPMAC_QUEUES + priv->ring_size),
  928. priv->desc_ring, priv->dma_ring);
  929. return 0;
  930. }
  931. static const struct net_device_ops cpmac_netdev_ops = {
  932. .ndo_open = cpmac_open,
  933. .ndo_stop = cpmac_stop,
  934. .ndo_start_xmit = cpmac_start_xmit,
  935. .ndo_tx_timeout = cpmac_tx_timeout,
  936. .ndo_set_rx_mode = cpmac_set_multicast_list,
  937. .ndo_do_ioctl = cpmac_ioctl,
  938. .ndo_change_mtu = eth_change_mtu,
  939. .ndo_validate_addr = eth_validate_addr,
  940. .ndo_set_mac_address = eth_mac_addr,
  941. };
  942. static int external_switch;
  943. static int cpmac_probe(struct platform_device *pdev)
  944. {
  945. int rc, phy_id;
  946. char mdio_bus_id[MII_BUS_ID_SIZE];
  947. struct resource *mem;
  948. struct cpmac_priv *priv;
  949. struct net_device *dev;
  950. struct plat_cpmac_data *pdata;
  951. pdata = dev_get_platdata(&pdev->dev);
  952. if (external_switch || dumb_switch) {
  953. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  954. phy_id = pdev->id;
  955. } else {
  956. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  957. if (!(pdata->phy_mask & (1 << phy_id)))
  958. continue;
  959. if (!cpmac_mii->phy_map[phy_id])
  960. continue;
  961. strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
  962. break;
  963. }
  964. }
  965. if (phy_id == PHY_MAX_ADDR) {
  966. dev_err(&pdev->dev, "no PHY present, falling back "
  967. "to switch on MDIO bus 0\n");
  968. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  969. phy_id = pdev->id;
  970. }
  971. mdio_bus_id[sizeof(mdio_bus_id) - 1] = '\0';
  972. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  973. if (!dev)
  974. return -ENOMEM;
  975. platform_set_drvdata(pdev, dev);
  976. priv = netdev_priv(dev);
  977. priv->pdev = pdev;
  978. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  979. if (!mem) {
  980. rc = -ENODEV;
  981. goto out;
  982. }
  983. dev->irq = platform_get_irq_byname(pdev, "irq");
  984. dev->netdev_ops = &cpmac_netdev_ops;
  985. dev->ethtool_ops = &cpmac_ethtool_ops;
  986. netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
  987. spin_lock_init(&priv->lock);
  988. spin_lock_init(&priv->rx_lock);
  989. priv->dev = dev;
  990. priv->ring_size = 64;
  991. priv->msg_enable = netif_msg_init(debug_level, 0xff);
  992. memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
  993. snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
  994. mdio_bus_id, phy_id);
  995. priv->phy = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
  996. PHY_INTERFACE_MODE_MII);
  997. if (IS_ERR(priv->phy)) {
  998. if (netif_msg_drv(priv))
  999. dev_err(&pdev->dev, "Could not attach to PHY\n");
  1000. rc = PTR_ERR(priv->phy);
  1001. goto out;
  1002. }
  1003. rc = register_netdev(dev);
  1004. if (rc) {
  1005. dev_err(&pdev->dev, "Could not register net device\n");
  1006. goto fail;
  1007. }
  1008. if (netif_msg_probe(priv)) {
  1009. dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
  1010. "mac: %pM\n", (void *)mem->start, dev->irq,
  1011. priv->phy_name, dev->dev_addr);
  1012. }
  1013. return 0;
  1014. fail:
  1015. free_netdev(dev);
  1016. out:
  1017. return rc;
  1018. }
  1019. static int cpmac_remove(struct platform_device *pdev)
  1020. {
  1021. struct net_device *dev = platform_get_drvdata(pdev);
  1022. unregister_netdev(dev);
  1023. free_netdev(dev);
  1024. return 0;
  1025. }
  1026. static struct platform_driver cpmac_driver = {
  1027. .driver = {
  1028. .name = "cpmac",
  1029. },
  1030. .probe = cpmac_probe,
  1031. .remove = cpmac_remove,
  1032. };
  1033. int cpmac_init(void)
  1034. {
  1035. u32 mask;
  1036. int i, res;
  1037. cpmac_mii = mdiobus_alloc();
  1038. if (cpmac_mii == NULL)
  1039. return -ENOMEM;
  1040. cpmac_mii->name = "cpmac-mii";
  1041. cpmac_mii->read = cpmac_mdio_read;
  1042. cpmac_mii->write = cpmac_mdio_write;
  1043. cpmac_mii->reset = cpmac_mdio_reset;
  1044. cpmac_mii->irq = mii_irqs;
  1045. cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
  1046. if (!cpmac_mii->priv) {
  1047. pr_err("Can't ioremap mdio registers\n");
  1048. res = -ENXIO;
  1049. goto fail_alloc;
  1050. }
  1051. #warning FIXME: unhardcode gpio&reset bits
  1052. ar7_gpio_disable(26);
  1053. ar7_gpio_disable(27);
  1054. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  1055. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  1056. ar7_device_reset(AR7_RESET_BIT_EPHY);
  1057. cpmac_mii->reset(cpmac_mii);
  1058. for (i = 0; i < 300; i++) {
  1059. mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
  1060. if (mask)
  1061. break;
  1062. else
  1063. msleep(10);
  1064. }
  1065. mask &= 0x7fffffff;
  1066. if (mask & (mask - 1)) {
  1067. external_switch = 1;
  1068. mask = 0;
  1069. }
  1070. cpmac_mii->phy_mask = ~(mask | 0x80000000);
  1071. snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
  1072. res = mdiobus_register(cpmac_mii);
  1073. if (res)
  1074. goto fail_mii;
  1075. res = platform_driver_register(&cpmac_driver);
  1076. if (res)
  1077. goto fail_cpmac;
  1078. return 0;
  1079. fail_cpmac:
  1080. mdiobus_unregister(cpmac_mii);
  1081. fail_mii:
  1082. iounmap(cpmac_mii->priv);
  1083. fail_alloc:
  1084. mdiobus_free(cpmac_mii);
  1085. return res;
  1086. }
  1087. void cpmac_exit(void)
  1088. {
  1089. platform_driver_unregister(&cpmac_driver);
  1090. mdiobus_unregister(cpmac_mii);
  1091. iounmap(cpmac_mii->priv);
  1092. mdiobus_free(cpmac_mii);
  1093. }
  1094. module_init(cpmac_init);
  1095. module_exit(cpmac_exit);