cpmac.c 35 KB

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