cpmac.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. /*
  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. struct phy_device *phy;
  193. char phy_name[MII_BUS_ID_SIZE + 3];
  194. int oldlink, oldspeed, oldduplex;
  195. u32 msg_enable;
  196. struct net_device *dev;
  197. struct work_struct reset_work;
  198. struct platform_device *pdev;
  199. struct napi_struct napi;
  200. atomic_t reset_pending;
  201. };
  202. static irqreturn_t cpmac_irq(int, void *);
  203. static void cpmac_hw_start(struct net_device *dev);
  204. static void cpmac_hw_stop(struct net_device *dev);
  205. static int cpmac_stop(struct net_device *dev);
  206. static int cpmac_open(struct net_device *dev);
  207. static void cpmac_dump_regs(struct net_device *dev)
  208. {
  209. int i;
  210. struct cpmac_priv *priv = netdev_priv(dev);
  211. for (i = 0; i < CPMAC_REG_END; i += 4) {
  212. if (i % 16 == 0) {
  213. if (i)
  214. pr_cont("\n");
  215. printk(KERN_DEBUG "%s: reg[%p]:", dev->name,
  216. priv->regs + i);
  217. }
  218. printk(" %08x", cpmac_read(priv->regs, i));
  219. }
  220. printk("\n");
  221. }
  222. static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
  223. {
  224. int i;
  225. printk(KERN_DEBUG "%s: desc[%p]:", dev->name, desc);
  226. for (i = 0; i < sizeof(*desc) / 4; i++)
  227. printk(" %08x", ((u32 *)desc)[i]);
  228. printk("\n");
  229. }
  230. static void cpmac_dump_all_desc(struct net_device *dev)
  231. {
  232. struct cpmac_priv *priv = netdev_priv(dev);
  233. struct cpmac_desc *dump = priv->rx_head;
  234. do {
  235. cpmac_dump_desc(dev, dump);
  236. dump = dump->next;
  237. } while (dump != priv->rx_head);
  238. }
  239. static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
  240. {
  241. int i;
  242. printk(KERN_DEBUG "%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
  243. for (i = 0; i < skb->len; i++) {
  244. if (i % 16 == 0) {
  245. if (i)
  246. pr_cont("\n");
  247. printk(KERN_DEBUG "%s: data[%p]:", dev->name,
  248. skb->data + i);
  249. }
  250. printk(" %02x", ((u8 *)skb->data)[i]);
  251. }
  252. printk("\n");
  253. }
  254. static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  255. {
  256. u32 val;
  257. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  258. cpu_relax();
  259. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
  260. MDIO_PHY(phy_id));
  261. while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
  262. cpu_relax();
  263. return MDIO_DATA(val);
  264. }
  265. static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
  266. int reg, u16 val)
  267. {
  268. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  269. cpu_relax();
  270. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
  271. MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
  272. return 0;
  273. }
  274. static int cpmac_mdio_reset(struct mii_bus *bus)
  275. {
  276. struct clk *cpmac_clk;
  277. cpmac_clk = clk_get(&bus->dev, "cpmac");
  278. if (IS_ERR(cpmac_clk)) {
  279. printk(KERN_ERR "unable to get cpmac clock\n");
  280. return -1;
  281. }
  282. ar7_device_reset(AR7_RESET_BIT_MDIO);
  283. cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
  284. MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
  285. return 0;
  286. }
  287. static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
  288. static struct mii_bus *cpmac_mii;
  289. static void cpmac_set_multicast_list(struct net_device *dev)
  290. {
  291. struct netdev_hw_addr *ha;
  292. u8 tmp;
  293. u32 mbp, bit, hash[2] = { 0, };
  294. struct cpmac_priv *priv = netdev_priv(dev);
  295. mbp = cpmac_read(priv->regs, CPMAC_MBP);
  296. if (dev->flags & IFF_PROMISC) {
  297. cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
  298. MBP_RXPROMISC);
  299. } else {
  300. cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
  301. if (dev->flags & IFF_ALLMULTI) {
  302. /* enable all multicast mode */
  303. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
  304. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
  305. } else {
  306. /*
  307. * cpmac uses some strange mac address hashing
  308. * (not crc32)
  309. */
  310. netdev_for_each_mc_addr(ha, dev) {
  311. bit = 0;
  312. tmp = ha->addr[0];
  313. bit ^= (tmp >> 2) ^ (tmp << 4);
  314. tmp = ha->addr[1];
  315. bit ^= (tmp >> 4) ^ (tmp << 2);
  316. tmp = ha->addr[2];
  317. bit ^= (tmp >> 6) ^ tmp;
  318. tmp = ha->addr[3];
  319. bit ^= (tmp >> 2) ^ (tmp << 4);
  320. tmp = ha->addr[4];
  321. bit ^= (tmp >> 4) ^ (tmp << 2);
  322. tmp = ha->addr[5];
  323. bit ^= (tmp >> 6) ^ tmp;
  324. bit &= 0x3f;
  325. hash[bit / 32] |= 1 << (bit % 32);
  326. }
  327. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
  328. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
  329. }
  330. }
  331. }
  332. static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
  333. struct cpmac_desc *desc)
  334. {
  335. struct sk_buff *skb, *result = NULL;
  336. if (unlikely(netif_msg_hw(priv)))
  337. cpmac_dump_desc(priv->dev, desc);
  338. cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
  339. if (unlikely(!desc->datalen)) {
  340. if (netif_msg_rx_err(priv) && net_ratelimit())
  341. printk(KERN_WARNING "%s: rx: spurious interrupt\n",
  342. priv->dev->name);
  343. return NULL;
  344. }
  345. skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
  346. if (likely(skb)) {
  347. skb_put(desc->skb, desc->datalen);
  348. desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
  349. skb_checksum_none_assert(desc->skb);
  350. priv->dev->stats.rx_packets++;
  351. priv->dev->stats.rx_bytes += desc->datalen;
  352. result = desc->skb;
  353. dma_unmap_single(&priv->dev->dev, desc->data_mapping,
  354. CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
  355. desc->skb = skb;
  356. desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
  357. CPMAC_SKB_SIZE,
  358. DMA_FROM_DEVICE);
  359. desc->hw_data = (u32)desc->data_mapping;
  360. if (unlikely(netif_msg_pktdata(priv))) {
  361. printk(KERN_DEBUG "%s: received packet:\n",
  362. priv->dev->name);
  363. cpmac_dump_skb(priv->dev, result);
  364. }
  365. } else {
  366. if (netif_msg_rx_err(priv) && net_ratelimit())
  367. printk(KERN_WARNING
  368. "%s: low on skbs, dropping packet\n",
  369. priv->dev->name);
  370. priv->dev->stats.rx_dropped++;
  371. }
  372. desc->buflen = CPMAC_SKB_SIZE;
  373. desc->dataflags = CPMAC_OWN;
  374. return result;
  375. }
  376. static int cpmac_poll(struct napi_struct *napi, int budget)
  377. {
  378. struct sk_buff *skb;
  379. struct cpmac_desc *desc, *restart;
  380. struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
  381. int received = 0, processed = 0;
  382. spin_lock(&priv->rx_lock);
  383. if (unlikely(!priv->rx_head)) {
  384. if (netif_msg_rx_err(priv) && net_ratelimit())
  385. printk(KERN_WARNING "%s: rx: polling, but no queue\n",
  386. priv->dev->name);
  387. spin_unlock(&priv->rx_lock);
  388. napi_complete(napi);
  389. return 0;
  390. }
  391. desc = priv->rx_head;
  392. restart = NULL;
  393. while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
  394. processed++;
  395. if ((desc->dataflags & CPMAC_EOQ) != 0) {
  396. /* The last update to eoq->hw_next didn't happen
  397. * soon enough, and the receiver stopped here.
  398. *Remember this descriptor so we can restart
  399. * the receiver after freeing some space.
  400. */
  401. if (unlikely(restart)) {
  402. if (netif_msg_rx_err(priv))
  403. printk(KERN_ERR "%s: poll found a"
  404. " duplicate EOQ: %p and %p\n",
  405. priv->dev->name, restart, desc);
  406. goto fatal_error;
  407. }
  408. restart = desc->next;
  409. }
  410. skb = cpmac_rx_one(priv, desc);
  411. if (likely(skb)) {
  412. netif_receive_skb(skb);
  413. received++;
  414. }
  415. desc = desc->next;
  416. }
  417. if (desc != priv->rx_head) {
  418. /* We freed some buffers, but not the whole ring,
  419. * add what we did free to the rx list */
  420. desc->prev->hw_next = (u32)0;
  421. priv->rx_head->prev->hw_next = priv->rx_head->mapping;
  422. }
  423. /* Optimization: If we did not actually process an EOQ (perhaps because
  424. * of quota limits), check to see if the tail of the queue has EOQ set.
  425. * We should immediately restart in that case so that the receiver can
  426. * restart and run in parallel with more packet processing.
  427. * This lets us handle slightly larger bursts before running
  428. * out of ring space (assuming dev->weight < ring_size) */
  429. if (!restart &&
  430. (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
  431. == CPMAC_EOQ &&
  432. (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
  433. /* reset EOQ so the poll loop (above) doesn't try to
  434. * restart this when it eventually gets to this descriptor.
  435. */
  436. priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
  437. restart = priv->rx_head;
  438. }
  439. if (restart) {
  440. priv->dev->stats.rx_errors++;
  441. priv->dev->stats.rx_fifo_errors++;
  442. if (netif_msg_rx_err(priv) && net_ratelimit())
  443. printk(KERN_WARNING "%s: rx dma ring overrun\n",
  444. priv->dev->name);
  445. if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
  446. if (netif_msg_drv(priv))
  447. printk(KERN_ERR "%s: cpmac_poll is trying to "
  448. "restart rx from a descriptor that's "
  449. "not free: %p\n",
  450. priv->dev->name, restart);
  451. goto fatal_error;
  452. }
  453. cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
  454. }
  455. priv->rx_head = desc;
  456. spin_unlock(&priv->rx_lock);
  457. if (unlikely(netif_msg_rx_status(priv)))
  458. printk(KERN_DEBUG "%s: poll processed %d packets\n",
  459. priv->dev->name, received);
  460. if (processed == 0) {
  461. /* we ran out of packets to read,
  462. * revert to interrupt-driven mode */
  463. napi_complete(napi);
  464. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  465. return 0;
  466. }
  467. return 1;
  468. fatal_error:
  469. /* Something went horribly wrong.
  470. * Reset hardware to try to recover rather than wedging. */
  471. if (netif_msg_drv(priv)) {
  472. printk(KERN_ERR "%s: cpmac_poll is confused. "
  473. "Resetting hardware\n", priv->dev->name);
  474. cpmac_dump_all_desc(priv->dev);
  475. printk(KERN_DEBUG "%s: RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
  476. priv->dev->name,
  477. cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
  478. cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
  479. }
  480. spin_unlock(&priv->rx_lock);
  481. napi_complete(napi);
  482. netif_tx_stop_all_queues(priv->dev);
  483. napi_disable(&priv->napi);
  484. atomic_inc(&priv->reset_pending);
  485. cpmac_hw_stop(priv->dev);
  486. if (!schedule_work(&priv->reset_work))
  487. atomic_dec(&priv->reset_pending);
  488. return 0;
  489. }
  490. static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  491. {
  492. int queue, len;
  493. struct cpmac_desc *desc;
  494. struct cpmac_priv *priv = netdev_priv(dev);
  495. if (unlikely(atomic_read(&priv->reset_pending)))
  496. return NETDEV_TX_BUSY;
  497. if (unlikely(skb_padto(skb, ETH_ZLEN)))
  498. return NETDEV_TX_OK;
  499. len = max(skb->len, ETH_ZLEN);
  500. queue = skb_get_queue_mapping(skb);
  501. netif_stop_subqueue(dev, queue);
  502. desc = &priv->desc_ring[queue];
  503. if (unlikely(desc->dataflags & CPMAC_OWN)) {
  504. if (netif_msg_tx_err(priv) && net_ratelimit())
  505. printk(KERN_WARNING "%s: tx dma ring full\n",
  506. dev->name);
  507. return NETDEV_TX_BUSY;
  508. }
  509. spin_lock(&priv->lock);
  510. spin_unlock(&priv->lock);
  511. desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
  512. desc->skb = skb;
  513. desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
  514. DMA_TO_DEVICE);
  515. desc->hw_data = (u32)desc->data_mapping;
  516. desc->datalen = len;
  517. desc->buflen = len;
  518. if (unlikely(netif_msg_tx_queued(priv)))
  519. printk(KERN_DEBUG "%s: sending 0x%p, len=%d\n", dev->name, skb,
  520. skb->len);
  521. if (unlikely(netif_msg_hw(priv)))
  522. cpmac_dump_desc(dev, desc);
  523. if (unlikely(netif_msg_pktdata(priv)))
  524. cpmac_dump_skb(dev, skb);
  525. cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
  526. return NETDEV_TX_OK;
  527. }
  528. static void cpmac_end_xmit(struct net_device *dev, int queue)
  529. {
  530. struct cpmac_desc *desc;
  531. struct cpmac_priv *priv = netdev_priv(dev);
  532. desc = &priv->desc_ring[queue];
  533. cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
  534. if (likely(desc->skb)) {
  535. spin_lock(&priv->lock);
  536. dev->stats.tx_packets++;
  537. dev->stats.tx_bytes += desc->skb->len;
  538. spin_unlock(&priv->lock);
  539. dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
  540. DMA_TO_DEVICE);
  541. if (unlikely(netif_msg_tx_done(priv)))
  542. printk(KERN_DEBUG "%s: sent 0x%p, len=%d\n", dev->name,
  543. desc->skb, desc->skb->len);
  544. dev_kfree_skb_irq(desc->skb);
  545. desc->skb = NULL;
  546. if (__netif_subqueue_stopped(dev, queue))
  547. netif_wake_subqueue(dev, queue);
  548. } else {
  549. if (netif_msg_tx_err(priv) && net_ratelimit())
  550. printk(KERN_WARNING
  551. "%s: end_xmit: spurious interrupt\n", dev->name);
  552. if (__netif_subqueue_stopped(dev, queue))
  553. netif_wake_subqueue(dev, queue);
  554. }
  555. }
  556. static void cpmac_hw_stop(struct net_device *dev)
  557. {
  558. int i;
  559. struct cpmac_priv *priv = netdev_priv(dev);
  560. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  561. ar7_device_reset(pdata->reset_bit);
  562. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  563. cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
  564. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  565. cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
  566. for (i = 0; i < 8; i++) {
  567. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  568. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  569. }
  570. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  571. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  572. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  573. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  574. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  575. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
  576. }
  577. static void cpmac_hw_start(struct net_device *dev)
  578. {
  579. int i;
  580. struct cpmac_priv *priv = netdev_priv(dev);
  581. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  582. ar7_device_reset(pdata->reset_bit);
  583. for (i = 0; i < 8; i++) {
  584. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  585. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  586. }
  587. cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
  588. cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
  589. MBP_RXMCAST);
  590. cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
  591. for (i = 0; i < 8; i++)
  592. cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
  593. cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
  594. cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
  595. (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
  596. (dev->dev_addr[3] << 24));
  597. cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
  598. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  599. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  600. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  601. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  602. cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
  603. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  604. cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
  605. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  606. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  607. cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
  608. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  609. cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
  610. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  611. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
  612. MAC_FDX);
  613. }
  614. static void cpmac_clear_rx(struct net_device *dev)
  615. {
  616. struct cpmac_priv *priv = netdev_priv(dev);
  617. struct cpmac_desc *desc;
  618. int i;
  619. if (unlikely(!priv->rx_head))
  620. return;
  621. desc = priv->rx_head;
  622. for (i = 0; i < priv->ring_size; i++) {
  623. if ((desc->dataflags & CPMAC_OWN) == 0) {
  624. if (netif_msg_rx_err(priv) && net_ratelimit())
  625. printk(KERN_WARNING "%s: packet dropped\n",
  626. dev->name);
  627. if (unlikely(netif_msg_hw(priv)))
  628. cpmac_dump_desc(dev, desc);
  629. desc->dataflags = CPMAC_OWN;
  630. dev->stats.rx_dropped++;
  631. }
  632. desc->hw_next = desc->next->mapping;
  633. desc = desc->next;
  634. }
  635. priv->rx_head->prev->hw_next = 0;
  636. }
  637. static void cpmac_clear_tx(struct net_device *dev)
  638. {
  639. struct cpmac_priv *priv = netdev_priv(dev);
  640. int i;
  641. if (unlikely(!priv->desc_ring))
  642. return;
  643. for (i = 0; i < CPMAC_QUEUES; i++) {
  644. priv->desc_ring[i].dataflags = 0;
  645. if (priv->desc_ring[i].skb) {
  646. dev_kfree_skb_any(priv->desc_ring[i].skb);
  647. priv->desc_ring[i].skb = NULL;
  648. }
  649. }
  650. }
  651. static void cpmac_hw_error(struct work_struct *work)
  652. {
  653. struct cpmac_priv *priv =
  654. container_of(work, struct cpmac_priv, reset_work);
  655. spin_lock(&priv->rx_lock);
  656. cpmac_clear_rx(priv->dev);
  657. spin_unlock(&priv->rx_lock);
  658. cpmac_clear_tx(priv->dev);
  659. cpmac_hw_start(priv->dev);
  660. barrier();
  661. atomic_dec(&priv->reset_pending);
  662. netif_tx_wake_all_queues(priv->dev);
  663. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  664. }
  665. static void cpmac_check_status(struct net_device *dev)
  666. {
  667. struct cpmac_priv *priv = netdev_priv(dev);
  668. u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
  669. int rx_channel = (macstatus >> 8) & 7;
  670. int rx_code = (macstatus >> 12) & 15;
  671. int tx_channel = (macstatus >> 16) & 7;
  672. int tx_code = (macstatus >> 20) & 15;
  673. if (rx_code || tx_code) {
  674. if (netif_msg_drv(priv) && net_ratelimit()) {
  675. /* Can't find any documentation on what these
  676. *error codes actually are. So just log them and hope..
  677. */
  678. if (rx_code)
  679. printk(KERN_WARNING "%s: host error %d on rx "
  680. "channel %d (macstatus %08x), resetting\n",
  681. dev->name, rx_code, rx_channel, macstatus);
  682. if (tx_code)
  683. printk(KERN_WARNING "%s: host error %d on tx "
  684. "channel %d (macstatus %08x), resetting\n",
  685. dev->name, tx_code, tx_channel, macstatus);
  686. }
  687. netif_tx_stop_all_queues(dev);
  688. cpmac_hw_stop(dev);
  689. if (schedule_work(&priv->reset_work))
  690. atomic_inc(&priv->reset_pending);
  691. if (unlikely(netif_msg_hw(priv)))
  692. cpmac_dump_regs(dev);
  693. }
  694. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  695. }
  696. static irqreturn_t cpmac_irq(int irq, void *dev_id)
  697. {
  698. struct net_device *dev = dev_id;
  699. struct cpmac_priv *priv;
  700. int queue;
  701. u32 status;
  702. priv = netdev_priv(dev);
  703. status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
  704. if (unlikely(netif_msg_intr(priv)))
  705. printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
  706. status);
  707. if (status & MAC_INT_TX)
  708. cpmac_end_xmit(dev, (status & 7));
  709. if (status & MAC_INT_RX) {
  710. queue = (status >> 8) & 7;
  711. if (napi_schedule_prep(&priv->napi)) {
  712. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
  713. __napi_schedule(&priv->napi);
  714. }
  715. }
  716. cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
  717. if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
  718. cpmac_check_status(dev);
  719. return IRQ_HANDLED;
  720. }
  721. static void cpmac_tx_timeout(struct net_device *dev)
  722. {
  723. struct cpmac_priv *priv = netdev_priv(dev);
  724. spin_lock(&priv->lock);
  725. dev->stats.tx_errors++;
  726. spin_unlock(&priv->lock);
  727. if (netif_msg_tx_err(priv) && net_ratelimit())
  728. printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
  729. atomic_inc(&priv->reset_pending);
  730. barrier();
  731. cpmac_clear_tx(dev);
  732. barrier();
  733. atomic_dec(&priv->reset_pending);
  734. netif_tx_wake_all_queues(priv->dev);
  735. }
  736. static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  737. {
  738. struct cpmac_priv *priv = netdev_priv(dev);
  739. if (!(netif_running(dev)))
  740. return -EINVAL;
  741. if (!priv->phy)
  742. return -EINVAL;
  743. return phy_mii_ioctl(priv->phy, ifr, cmd);
  744. }
  745. static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  746. {
  747. struct cpmac_priv *priv = netdev_priv(dev);
  748. if (priv->phy)
  749. return phy_ethtool_gset(priv->phy, cmd);
  750. return -EINVAL;
  751. }
  752. static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  753. {
  754. struct cpmac_priv *priv = netdev_priv(dev);
  755. if (!capable(CAP_NET_ADMIN))
  756. return -EPERM;
  757. if (priv->phy)
  758. return phy_ethtool_sset(priv->phy, cmd);
  759. return -EINVAL;
  760. }
  761. static void cpmac_get_ringparam(struct net_device *dev,
  762. struct ethtool_ringparam *ring)
  763. {
  764. struct cpmac_priv *priv = netdev_priv(dev);
  765. ring->rx_max_pending = 1024;
  766. ring->rx_mini_max_pending = 1;
  767. ring->rx_jumbo_max_pending = 1;
  768. ring->tx_max_pending = 1;
  769. ring->rx_pending = priv->ring_size;
  770. ring->rx_mini_pending = 1;
  771. ring->rx_jumbo_pending = 1;
  772. ring->tx_pending = 1;
  773. }
  774. static int cpmac_set_ringparam(struct net_device *dev,
  775. struct ethtool_ringparam *ring)
  776. {
  777. struct cpmac_priv *priv = netdev_priv(dev);
  778. if (netif_running(dev))
  779. return -EBUSY;
  780. priv->ring_size = ring->rx_pending;
  781. return 0;
  782. }
  783. static void cpmac_get_drvinfo(struct net_device *dev,
  784. struct ethtool_drvinfo *info)
  785. {
  786. strlcpy(info->driver, "cpmac", sizeof(info->driver));
  787. strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
  788. snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
  789. info->regdump_len = 0;
  790. }
  791. static const struct ethtool_ops cpmac_ethtool_ops = {
  792. .get_settings = cpmac_get_settings,
  793. .set_settings = cpmac_set_settings,
  794. .get_drvinfo = cpmac_get_drvinfo,
  795. .get_link = ethtool_op_get_link,
  796. .get_ringparam = cpmac_get_ringparam,
  797. .set_ringparam = cpmac_set_ringparam,
  798. };
  799. static void cpmac_adjust_link(struct net_device *dev)
  800. {
  801. struct cpmac_priv *priv = netdev_priv(dev);
  802. int new_state = 0;
  803. spin_lock(&priv->lock);
  804. if (priv->phy->link) {
  805. netif_tx_start_all_queues(dev);
  806. if (priv->phy->duplex != priv->oldduplex) {
  807. new_state = 1;
  808. priv->oldduplex = priv->phy->duplex;
  809. }
  810. if (priv->phy->speed != priv->oldspeed) {
  811. new_state = 1;
  812. priv->oldspeed = priv->phy->speed;
  813. }
  814. if (!priv->oldlink) {
  815. new_state = 1;
  816. priv->oldlink = 1;
  817. }
  818. } else if (priv->oldlink) {
  819. new_state = 1;
  820. priv->oldlink = 0;
  821. priv->oldspeed = 0;
  822. priv->oldduplex = -1;
  823. }
  824. if (new_state && netif_msg_link(priv) && net_ratelimit())
  825. phy_print_status(priv->phy);
  826. spin_unlock(&priv->lock);
  827. }
  828. static int cpmac_open(struct net_device *dev)
  829. {
  830. int i, size, res;
  831. struct cpmac_priv *priv = netdev_priv(dev);
  832. struct resource *mem;
  833. struct cpmac_desc *desc;
  834. struct sk_buff *skb;
  835. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  836. if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
  837. if (netif_msg_drv(priv))
  838. printk(KERN_ERR "%s: failed to request registers\n",
  839. dev->name);
  840. res = -ENXIO;
  841. goto fail_reserve;
  842. }
  843. priv->regs = ioremap(mem->start, resource_size(mem));
  844. if (!priv->regs) {
  845. if (netif_msg_drv(priv))
  846. printk(KERN_ERR "%s: failed to remap registers\n",
  847. dev->name);
  848. res = -ENXIO;
  849. goto fail_remap;
  850. }
  851. size = priv->ring_size + CPMAC_QUEUES;
  852. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  853. sizeof(struct cpmac_desc) * size,
  854. &priv->dma_ring,
  855. GFP_KERNEL);
  856. if (!priv->desc_ring) {
  857. res = -ENOMEM;
  858. goto fail_alloc;
  859. }
  860. for (i = 0; i < size; i++)
  861. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  862. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  863. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  864. skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
  865. if (unlikely(!skb)) {
  866. res = -ENOMEM;
  867. goto fail_desc;
  868. }
  869. desc->skb = skb;
  870. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  871. CPMAC_SKB_SIZE,
  872. DMA_FROM_DEVICE);
  873. desc->hw_data = (u32)desc->data_mapping;
  874. desc->buflen = CPMAC_SKB_SIZE;
  875. desc->dataflags = CPMAC_OWN;
  876. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  877. desc->next->prev = desc;
  878. desc->hw_next = (u32)desc->next->mapping;
  879. }
  880. priv->rx_head->prev->hw_next = (u32)0;
  881. res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
  882. if (res) {
  883. if (netif_msg_drv(priv))
  884. printk(KERN_ERR "%s: failed to obtain irq\n",
  885. dev->name);
  886. goto fail_irq;
  887. }
  888. atomic_set(&priv->reset_pending, 0);
  889. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  890. cpmac_hw_start(dev);
  891. napi_enable(&priv->napi);
  892. priv->phy->state = PHY_CHANGELINK;
  893. phy_start(priv->phy);
  894. return 0;
  895. fail_irq:
  896. fail_desc:
  897. for (i = 0; i < priv->ring_size; i++) {
  898. if (priv->rx_head[i].skb) {
  899. dma_unmap_single(&dev->dev,
  900. priv->rx_head[i].data_mapping,
  901. CPMAC_SKB_SIZE,
  902. DMA_FROM_DEVICE);
  903. kfree_skb(priv->rx_head[i].skb);
  904. }
  905. }
  906. fail_alloc:
  907. kfree(priv->desc_ring);
  908. iounmap(priv->regs);
  909. fail_remap:
  910. release_mem_region(mem->start, resource_size(mem));
  911. fail_reserve:
  912. return res;
  913. }
  914. static int cpmac_stop(struct net_device *dev)
  915. {
  916. int i;
  917. struct cpmac_priv *priv = netdev_priv(dev);
  918. struct resource *mem;
  919. netif_tx_stop_all_queues(dev);
  920. cancel_work_sync(&priv->reset_work);
  921. napi_disable(&priv->napi);
  922. phy_stop(priv->phy);
  923. cpmac_hw_stop(dev);
  924. for (i = 0; i < 8; i++)
  925. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  926. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  927. cpmac_write(priv->regs, CPMAC_MBP, 0);
  928. free_irq(dev->irq, dev);
  929. iounmap(priv->regs);
  930. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  931. release_mem_region(mem->start, resource_size(mem));
  932. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  933. for (i = 0; i < priv->ring_size; i++) {
  934. if (priv->rx_head[i].skb) {
  935. dma_unmap_single(&dev->dev,
  936. priv->rx_head[i].data_mapping,
  937. CPMAC_SKB_SIZE,
  938. DMA_FROM_DEVICE);
  939. kfree_skb(priv->rx_head[i].skb);
  940. }
  941. }
  942. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  943. (CPMAC_QUEUES + priv->ring_size),
  944. priv->desc_ring, priv->dma_ring);
  945. return 0;
  946. }
  947. static const struct net_device_ops cpmac_netdev_ops = {
  948. .ndo_open = cpmac_open,
  949. .ndo_stop = cpmac_stop,
  950. .ndo_start_xmit = cpmac_start_xmit,
  951. .ndo_tx_timeout = cpmac_tx_timeout,
  952. .ndo_set_rx_mode = cpmac_set_multicast_list,
  953. .ndo_do_ioctl = cpmac_ioctl,
  954. .ndo_change_mtu = eth_change_mtu,
  955. .ndo_validate_addr = eth_validate_addr,
  956. .ndo_set_mac_address = eth_mac_addr,
  957. };
  958. static int external_switch;
  959. static int cpmac_probe(struct platform_device *pdev)
  960. {
  961. int rc, phy_id;
  962. char mdio_bus_id[MII_BUS_ID_SIZE];
  963. struct resource *mem;
  964. struct cpmac_priv *priv;
  965. struct net_device *dev;
  966. struct plat_cpmac_data *pdata;
  967. pdata = dev_get_platdata(&pdev->dev);
  968. if (external_switch || dumb_switch) {
  969. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  970. phy_id = pdev->id;
  971. } else {
  972. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  973. if (!(pdata->phy_mask & (1 << phy_id)))
  974. continue;
  975. if (!cpmac_mii->phy_map[phy_id])
  976. continue;
  977. strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
  978. break;
  979. }
  980. }
  981. if (phy_id == PHY_MAX_ADDR) {
  982. dev_err(&pdev->dev, "no PHY present, falling back "
  983. "to switch on MDIO bus 0\n");
  984. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  985. phy_id = pdev->id;
  986. }
  987. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  988. if (!dev)
  989. return -ENOMEM;
  990. platform_set_drvdata(pdev, dev);
  991. priv = netdev_priv(dev);
  992. priv->pdev = pdev;
  993. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  994. if (!mem) {
  995. rc = -ENODEV;
  996. goto fail;
  997. }
  998. dev->irq = platform_get_irq_byname(pdev, "irq");
  999. dev->netdev_ops = &cpmac_netdev_ops;
  1000. dev->ethtool_ops = &cpmac_ethtool_ops;
  1001. netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
  1002. spin_lock_init(&priv->lock);
  1003. spin_lock_init(&priv->rx_lock);
  1004. priv->dev = dev;
  1005. priv->ring_size = 64;
  1006. priv->msg_enable = netif_msg_init(debug_level, 0xff);
  1007. memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
  1008. snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
  1009. mdio_bus_id, phy_id);
  1010. priv->phy = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
  1011. PHY_INTERFACE_MODE_MII);
  1012. if (IS_ERR(priv->phy)) {
  1013. if (netif_msg_drv(priv))
  1014. printk(KERN_ERR "%s: Could not attach to PHY\n",
  1015. dev->name);
  1016. rc = PTR_ERR(priv->phy);
  1017. goto fail;
  1018. }
  1019. rc = register_netdev(dev);
  1020. if (rc) {
  1021. printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
  1022. dev->name);
  1023. goto fail;
  1024. }
  1025. if (netif_msg_probe(priv)) {
  1026. printk(KERN_INFO
  1027. "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
  1028. "mac: %pM)\n", dev->name, (void *)mem->start, dev->irq,
  1029. priv->phy_name, dev->dev_addr);
  1030. }
  1031. return 0;
  1032. fail:
  1033. free_netdev(dev);
  1034. return rc;
  1035. }
  1036. static int cpmac_remove(struct platform_device *pdev)
  1037. {
  1038. struct net_device *dev = platform_get_drvdata(pdev);
  1039. unregister_netdev(dev);
  1040. free_netdev(dev);
  1041. return 0;
  1042. }
  1043. static struct platform_driver cpmac_driver = {
  1044. .driver.name = "cpmac",
  1045. .driver.owner = THIS_MODULE,
  1046. .probe = cpmac_probe,
  1047. .remove = cpmac_remove,
  1048. };
  1049. int cpmac_init(void)
  1050. {
  1051. u32 mask;
  1052. int i, res;
  1053. cpmac_mii = mdiobus_alloc();
  1054. if (cpmac_mii == NULL)
  1055. return -ENOMEM;
  1056. cpmac_mii->name = "cpmac-mii";
  1057. cpmac_mii->read = cpmac_mdio_read;
  1058. cpmac_mii->write = cpmac_mdio_write;
  1059. cpmac_mii->reset = cpmac_mdio_reset;
  1060. cpmac_mii->irq = mii_irqs;
  1061. cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
  1062. if (!cpmac_mii->priv) {
  1063. printk(KERN_ERR "Can't ioremap mdio registers\n");
  1064. res = -ENXIO;
  1065. goto fail_alloc;
  1066. }
  1067. #warning FIXME: unhardcode gpio&reset bits
  1068. ar7_gpio_disable(26);
  1069. ar7_gpio_disable(27);
  1070. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  1071. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  1072. ar7_device_reset(AR7_RESET_BIT_EPHY);
  1073. cpmac_mii->reset(cpmac_mii);
  1074. for (i = 0; i < 300; i++) {
  1075. mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
  1076. if (mask)
  1077. break;
  1078. else
  1079. msleep(10);
  1080. }
  1081. mask &= 0x7fffffff;
  1082. if (mask & (mask - 1)) {
  1083. external_switch = 1;
  1084. mask = 0;
  1085. }
  1086. cpmac_mii->phy_mask = ~(mask | 0x80000000);
  1087. snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
  1088. res = mdiobus_register(cpmac_mii);
  1089. if (res)
  1090. goto fail_mii;
  1091. res = platform_driver_register(&cpmac_driver);
  1092. if (res)
  1093. goto fail_cpmac;
  1094. return 0;
  1095. fail_cpmac:
  1096. mdiobus_unregister(cpmac_mii);
  1097. fail_mii:
  1098. iounmap(cpmac_mii->priv);
  1099. fail_alloc:
  1100. mdiobus_free(cpmac_mii);
  1101. return res;
  1102. }
  1103. void cpmac_exit(void)
  1104. {
  1105. platform_driver_unregister(&cpmac_driver);
  1106. mdiobus_unregister(cpmac_mii);
  1107. iounmap(cpmac_mii->priv);
  1108. mdiobus_free(cpmac_mii);
  1109. }
  1110. module_init(cpmac_init);
  1111. module_exit(cpmac_exit);