cpmac.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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/version.h>
  28. #include <linux/netdevice.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 <asm/gpio.h>
  38. MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
  39. MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
  40. MODULE_LICENSE("GPL");
  41. MODULE_ALIAS("platform:cpmac");
  42. static int debug_level = 8;
  43. static int dumb_switch;
  44. /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
  45. module_param(debug_level, int, 0444);
  46. module_param(dumb_switch, int, 0444);
  47. MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
  48. MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
  49. #define CPMAC_VERSION "0.5.0"
  50. /* frame size + 802.1q tag */
  51. #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + 4)
  52. #define CPMAC_QUEUES 8
  53. /* Ethernet registers */
  54. #define CPMAC_TX_CONTROL 0x0004
  55. #define CPMAC_TX_TEARDOWN 0x0008
  56. #define CPMAC_RX_CONTROL 0x0014
  57. #define CPMAC_RX_TEARDOWN 0x0018
  58. #define CPMAC_MBP 0x0100
  59. # define MBP_RXPASSCRC 0x40000000
  60. # define MBP_RXQOS 0x20000000
  61. # define MBP_RXNOCHAIN 0x10000000
  62. # define MBP_RXCMF 0x01000000
  63. # define MBP_RXSHORT 0x00800000
  64. # define MBP_RXCEF 0x00400000
  65. # define MBP_RXPROMISC 0x00200000
  66. # define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
  67. # define MBP_RXBCAST 0x00002000
  68. # define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
  69. # define MBP_RXMCAST 0x00000020
  70. # define MBP_MCASTCHAN(channel) ((channel) & 0x7)
  71. #define CPMAC_UNICAST_ENABLE 0x0104
  72. #define CPMAC_UNICAST_CLEAR 0x0108
  73. #define CPMAC_MAX_LENGTH 0x010c
  74. #define CPMAC_BUFFER_OFFSET 0x0110
  75. #define CPMAC_MAC_CONTROL 0x0160
  76. # define MAC_TXPTYPE 0x00000200
  77. # define MAC_TXPACE 0x00000040
  78. # define MAC_MII 0x00000020
  79. # define MAC_TXFLOW 0x00000010
  80. # define MAC_RXFLOW 0x00000008
  81. # define MAC_MTEST 0x00000004
  82. # define MAC_LOOPBACK 0x00000002
  83. # define MAC_FDX 0x00000001
  84. #define CPMAC_MAC_STATUS 0x0164
  85. # define MAC_STATUS_QOS 0x00000004
  86. # define MAC_STATUS_RXFLOW 0x00000002
  87. # define MAC_STATUS_TXFLOW 0x00000001
  88. #define CPMAC_TX_INT_ENABLE 0x0178
  89. #define CPMAC_TX_INT_CLEAR 0x017c
  90. #define CPMAC_MAC_INT_VECTOR 0x0180
  91. # define MAC_INT_STATUS 0x00080000
  92. # define MAC_INT_HOST 0x00040000
  93. # define MAC_INT_RX 0x00020000
  94. # define MAC_INT_TX 0x00010000
  95. #define CPMAC_MAC_EOI_VECTOR 0x0184
  96. #define CPMAC_RX_INT_ENABLE 0x0198
  97. #define CPMAC_RX_INT_CLEAR 0x019c
  98. #define CPMAC_MAC_INT_ENABLE 0x01a8
  99. #define CPMAC_MAC_INT_CLEAR 0x01ac
  100. #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
  101. #define CPMAC_MAC_ADDR_MID 0x01d0
  102. #define CPMAC_MAC_ADDR_HI 0x01d4
  103. #define CPMAC_MAC_HASH_LO 0x01d8
  104. #define CPMAC_MAC_HASH_HI 0x01dc
  105. #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
  106. #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
  107. #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
  108. #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
  109. #define CPMAC_REG_END 0x0680
  110. /*
  111. * Rx/Tx statistics
  112. * TODO: use some of them to fill stats in cpmac_stats()
  113. */
  114. #define CPMAC_STATS_RX_GOOD 0x0200
  115. #define CPMAC_STATS_RX_BCAST 0x0204
  116. #define CPMAC_STATS_RX_MCAST 0x0208
  117. #define CPMAC_STATS_RX_PAUSE 0x020c
  118. #define CPMAC_STATS_RX_CRC 0x0210
  119. #define CPMAC_STATS_RX_ALIGN 0x0214
  120. #define CPMAC_STATS_RX_OVER 0x0218
  121. #define CPMAC_STATS_RX_JABBER 0x021c
  122. #define CPMAC_STATS_RX_UNDER 0x0220
  123. #define CPMAC_STATS_RX_FRAG 0x0224
  124. #define CPMAC_STATS_RX_FILTER 0x0228
  125. #define CPMAC_STATS_RX_QOSFILTER 0x022c
  126. #define CPMAC_STATS_RX_OCTETS 0x0230
  127. #define CPMAC_STATS_TX_GOOD 0x0234
  128. #define CPMAC_STATS_TX_BCAST 0x0238
  129. #define CPMAC_STATS_TX_MCAST 0x023c
  130. #define CPMAC_STATS_TX_PAUSE 0x0240
  131. #define CPMAC_STATS_TX_DEFER 0x0244
  132. #define CPMAC_STATS_TX_COLLISION 0x0248
  133. #define CPMAC_STATS_TX_SINGLECOLL 0x024c
  134. #define CPMAC_STATS_TX_MULTICOLL 0x0250
  135. #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
  136. #define CPMAC_STATS_TX_LATECOLL 0x0258
  137. #define CPMAC_STATS_TX_UNDERRUN 0x025c
  138. #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
  139. #define CPMAC_STATS_TX_OCTETS 0x0264
  140. #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
  141. #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
  142. (reg)))
  143. /* MDIO bus */
  144. #define CPMAC_MDIO_VERSION 0x0000
  145. #define CPMAC_MDIO_CONTROL 0x0004
  146. # define MDIOC_IDLE 0x80000000
  147. # define MDIOC_ENABLE 0x40000000
  148. # define MDIOC_PREAMBLE 0x00100000
  149. # define MDIOC_FAULT 0x00080000
  150. # define MDIOC_FAULTDETECT 0x00040000
  151. # define MDIOC_INTTEST 0x00020000
  152. # define MDIOC_CLKDIV(div) ((div) & 0xff)
  153. #define CPMAC_MDIO_ALIVE 0x0008
  154. #define CPMAC_MDIO_LINK 0x000c
  155. #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
  156. # define MDIO_BUSY 0x80000000
  157. # define MDIO_WRITE 0x40000000
  158. # define MDIO_REG(reg) (((reg) & 0x1f) << 21)
  159. # define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
  160. # define MDIO_DATA(data) ((data) & 0xffff)
  161. #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
  162. # define PHYSEL_LINKSEL 0x00000040
  163. # define PHYSEL_LINKINT 0x00000020
  164. struct cpmac_desc {
  165. u32 hw_next;
  166. u32 hw_data;
  167. u16 buflen;
  168. u16 bufflags;
  169. u16 datalen;
  170. u16 dataflags;
  171. #define CPMAC_SOP 0x8000
  172. #define CPMAC_EOP 0x4000
  173. #define CPMAC_OWN 0x2000
  174. #define CPMAC_EOQ 0x1000
  175. struct sk_buff *skb;
  176. struct cpmac_desc *next;
  177. dma_addr_t mapping;
  178. dma_addr_t data_mapping;
  179. };
  180. struct cpmac_priv {
  181. spinlock_t lock;
  182. spinlock_t rx_lock;
  183. struct cpmac_desc *rx_head;
  184. int ring_size;
  185. struct cpmac_desc *desc_ring;
  186. dma_addr_t dma_ring;
  187. void __iomem *regs;
  188. struct mii_bus *mii_bus;
  189. struct phy_device *phy;
  190. char phy_name[BUS_ID_SIZE];
  191. int oldlink, oldspeed, oldduplex;
  192. u32 msg_enable;
  193. struct net_device *dev;
  194. struct work_struct reset_work;
  195. struct platform_device *pdev;
  196. struct napi_struct napi;
  197. };
  198. static irqreturn_t cpmac_irq(int, void *);
  199. static void cpmac_hw_start(struct net_device *dev);
  200. static void cpmac_hw_stop(struct net_device *dev);
  201. static int cpmac_stop(struct net_device *dev);
  202. static int cpmac_open(struct net_device *dev);
  203. static void cpmac_dump_regs(struct net_device *dev)
  204. {
  205. int i;
  206. struct cpmac_priv *priv = netdev_priv(dev);
  207. for (i = 0; i < CPMAC_REG_END; i += 4) {
  208. if (i % 16 == 0) {
  209. if (i)
  210. printk("\n");
  211. printk(KERN_DEBUG "%s: reg[%p]:", dev->name,
  212. priv->regs + i);
  213. }
  214. printk(" %08x", cpmac_read(priv->regs, i));
  215. }
  216. printk("\n");
  217. }
  218. static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
  219. {
  220. int i;
  221. printk(KERN_DEBUG "%s: desc[%p]:", dev->name, desc);
  222. for (i = 0; i < sizeof(*desc) / 4; i++)
  223. printk(" %08x", ((u32 *)desc)[i]);
  224. printk("\n");
  225. }
  226. static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
  227. {
  228. int i;
  229. printk(KERN_DEBUG "%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
  230. for (i = 0; i < skb->len; i++) {
  231. if (i % 16 == 0) {
  232. if (i)
  233. printk("\n");
  234. printk(KERN_DEBUG "%s: data[%p]:", dev->name,
  235. skb->data + i);
  236. }
  237. printk(" %02x", ((u8 *)skb->data)[i]);
  238. }
  239. printk("\n");
  240. }
  241. static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  242. {
  243. u32 val;
  244. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  245. cpu_relax();
  246. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
  247. MDIO_PHY(phy_id));
  248. while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
  249. cpu_relax();
  250. return MDIO_DATA(val);
  251. }
  252. static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
  253. int reg, u16 val)
  254. {
  255. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  256. cpu_relax();
  257. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
  258. MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
  259. return 0;
  260. }
  261. static int cpmac_mdio_reset(struct mii_bus *bus)
  262. {
  263. ar7_device_reset(AR7_RESET_BIT_MDIO);
  264. cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
  265. MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1));
  266. return 0;
  267. }
  268. static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
  269. static struct mii_bus cpmac_mii = {
  270. .name = "cpmac-mii",
  271. .read = cpmac_mdio_read,
  272. .write = cpmac_mdio_write,
  273. .reset = cpmac_mdio_reset,
  274. .irq = mii_irqs,
  275. };
  276. static int cpmac_config(struct net_device *dev, struct ifmap *map)
  277. {
  278. if (dev->flags & IFF_UP)
  279. return -EBUSY;
  280. /* Don't allow changing the I/O address */
  281. if (map->base_addr != dev->base_addr)
  282. return -EOPNOTSUPP;
  283. /* ignore other fields */
  284. return 0;
  285. }
  286. static void cpmac_set_multicast_list(struct net_device *dev)
  287. {
  288. struct dev_mc_list *iter;
  289. int i;
  290. u8 tmp;
  291. u32 mbp, bit, hash[2] = { 0, };
  292. struct cpmac_priv *priv = netdev_priv(dev);
  293. mbp = cpmac_read(priv->regs, CPMAC_MBP);
  294. if (dev->flags & IFF_PROMISC) {
  295. cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
  296. MBP_RXPROMISC);
  297. } else {
  298. cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
  299. if (dev->flags & IFF_ALLMULTI) {
  300. /* enable all multicast mode */
  301. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
  302. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
  303. } else {
  304. /*
  305. * cpmac uses some strange mac address hashing
  306. * (not crc32)
  307. */
  308. for (i = 0, iter = dev->mc_list; i < dev->mc_count;
  309. i++, iter = iter->next) {
  310. bit = 0;
  311. tmp = iter->dmi_addr[0];
  312. bit ^= (tmp >> 2) ^ (tmp << 4);
  313. tmp = iter->dmi_addr[1];
  314. bit ^= (tmp >> 4) ^ (tmp << 2);
  315. tmp = iter->dmi_addr[2];
  316. bit ^= (tmp >> 6) ^ tmp;
  317. tmp = iter->dmi_addr[3];
  318. bit ^= (tmp >> 2) ^ (tmp << 4);
  319. tmp = iter->dmi_addr[4];
  320. bit ^= (tmp >> 4) ^ (tmp << 2);
  321. tmp = iter->dmi_addr[5];
  322. bit ^= (tmp >> 6) ^ tmp;
  323. bit &= 0x3f;
  324. hash[bit / 32] |= 1 << (bit % 32);
  325. }
  326. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
  327. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
  328. }
  329. }
  330. }
  331. static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
  332. struct cpmac_desc *desc)
  333. {
  334. struct sk_buff *skb, *result = NULL;
  335. if (unlikely(netif_msg_hw(priv)))
  336. cpmac_dump_desc(priv->dev, desc);
  337. cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
  338. if (unlikely(!desc->datalen)) {
  339. if (netif_msg_rx_err(priv) && net_ratelimit())
  340. printk(KERN_WARNING "%s: rx: spurious interrupt\n",
  341. priv->dev->name);
  342. return NULL;
  343. }
  344. skb = netdev_alloc_skb(priv->dev, CPMAC_SKB_SIZE);
  345. if (likely(skb)) {
  346. skb_reserve(skb, 2);
  347. skb_put(desc->skb, desc->datalen);
  348. desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
  349. desc->skb->ip_summed = CHECKSUM_NONE;
  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;
  380. int received = 0;
  381. struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
  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. netif_rx_complete(priv->dev, napi);
  388. return 0;
  389. }
  390. desc = priv->rx_head;
  391. while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
  392. skb = cpmac_rx_one(priv, desc);
  393. if (likely(skb)) {
  394. netif_receive_skb(skb);
  395. received++;
  396. }
  397. desc = desc->next;
  398. }
  399. priv->rx_head = desc;
  400. spin_unlock(&priv->rx_lock);
  401. if (unlikely(netif_msg_rx_status(priv)))
  402. printk(KERN_DEBUG "%s: poll processed %d packets\n",
  403. priv->dev->name, received);
  404. if (desc->dataflags & CPMAC_OWN) {
  405. netif_rx_complete(priv->dev, napi);
  406. cpmac_write(priv->regs, CPMAC_RX_PTR(0), (u32)desc->mapping);
  407. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  408. return 0;
  409. }
  410. return 1;
  411. }
  412. static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  413. {
  414. int queue, len;
  415. struct cpmac_desc *desc;
  416. struct cpmac_priv *priv = netdev_priv(dev);
  417. if (unlikely(skb_padto(skb, ETH_ZLEN)))
  418. return NETDEV_TX_OK;
  419. len = max(skb->len, ETH_ZLEN);
  420. queue = skb_get_queue_mapping(skb);
  421. #ifdef CONFIG_NETDEVICES_MULTIQUEUE
  422. netif_stop_subqueue(dev, queue);
  423. #else
  424. netif_stop_queue(dev);
  425. #endif
  426. desc = &priv->desc_ring[queue];
  427. if (unlikely(desc->dataflags & CPMAC_OWN)) {
  428. if (netif_msg_tx_err(priv) && net_ratelimit())
  429. printk(KERN_WARNING "%s: tx dma ring full\n",
  430. dev->name);
  431. return NETDEV_TX_BUSY;
  432. }
  433. spin_lock(&priv->lock);
  434. dev->trans_start = jiffies;
  435. spin_unlock(&priv->lock);
  436. desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
  437. desc->skb = skb;
  438. desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
  439. DMA_TO_DEVICE);
  440. desc->hw_data = (u32)desc->data_mapping;
  441. desc->datalen = len;
  442. desc->buflen = len;
  443. if (unlikely(netif_msg_tx_queued(priv)))
  444. printk(KERN_DEBUG "%s: sending 0x%p, len=%d\n", dev->name, skb,
  445. skb->len);
  446. if (unlikely(netif_msg_hw(priv)))
  447. cpmac_dump_desc(dev, desc);
  448. if (unlikely(netif_msg_pktdata(priv)))
  449. cpmac_dump_skb(dev, skb);
  450. cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
  451. return NETDEV_TX_OK;
  452. }
  453. static void cpmac_end_xmit(struct net_device *dev, int queue)
  454. {
  455. struct cpmac_desc *desc;
  456. struct cpmac_priv *priv = netdev_priv(dev);
  457. desc = &priv->desc_ring[queue];
  458. cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
  459. if (likely(desc->skb)) {
  460. spin_lock(&priv->lock);
  461. dev->stats.tx_packets++;
  462. dev->stats.tx_bytes += desc->skb->len;
  463. spin_unlock(&priv->lock);
  464. dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
  465. DMA_TO_DEVICE);
  466. if (unlikely(netif_msg_tx_done(priv)))
  467. printk(KERN_DEBUG "%s: sent 0x%p, len=%d\n", dev->name,
  468. desc->skb, desc->skb->len);
  469. dev_kfree_skb_irq(desc->skb);
  470. desc->skb = NULL;
  471. #ifdef CONFIG_NETDEVICES_MULTIQUEUE
  472. if (netif_subqueue_stopped(dev, queue))
  473. netif_wake_subqueue(dev, queue);
  474. #else
  475. if (netif_queue_stopped(dev))
  476. netif_wake_queue(dev);
  477. #endif
  478. } else {
  479. if (netif_msg_tx_err(priv) && net_ratelimit())
  480. printk(KERN_WARNING
  481. "%s: end_xmit: spurious interrupt\n", dev->name);
  482. #ifdef CONFIG_NETDEVICES_MULTIQUEUE
  483. if (netif_subqueue_stopped(dev, queue))
  484. netif_wake_subqueue(dev, queue);
  485. #else
  486. if (netif_queue_stopped(dev))
  487. netif_wake_queue(dev);
  488. #endif
  489. }
  490. }
  491. static void cpmac_hw_stop(struct net_device *dev)
  492. {
  493. int i;
  494. struct cpmac_priv *priv = netdev_priv(dev);
  495. struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
  496. ar7_device_reset(pdata->reset_bit);
  497. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  498. cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
  499. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  500. cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
  501. for (i = 0; i < 8; i++) {
  502. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  503. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  504. }
  505. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  506. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  507. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  508. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  509. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  510. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
  511. }
  512. static void cpmac_hw_start(struct net_device *dev)
  513. {
  514. int i;
  515. struct cpmac_priv *priv = netdev_priv(dev);
  516. struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
  517. ar7_device_reset(pdata->reset_bit);
  518. for (i = 0; i < 8; i++) {
  519. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  520. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  521. }
  522. cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
  523. cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
  524. MBP_RXMCAST);
  525. cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
  526. for (i = 0; i < 8; i++)
  527. cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
  528. cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
  529. cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
  530. (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
  531. (dev->dev_addr[3] << 24));
  532. cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
  533. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  534. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  535. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  536. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  537. cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
  538. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  539. cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
  540. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  541. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  542. cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
  543. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  544. cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
  545. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  546. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
  547. MAC_FDX);
  548. }
  549. static void cpmac_clear_rx(struct net_device *dev)
  550. {
  551. struct cpmac_priv *priv = netdev_priv(dev);
  552. struct cpmac_desc *desc;
  553. int i;
  554. if (unlikely(!priv->rx_head))
  555. return;
  556. desc = priv->rx_head;
  557. for (i = 0; i < priv->ring_size; i++) {
  558. if ((desc->dataflags & CPMAC_OWN) == 0) {
  559. if (netif_msg_rx_err(priv) && net_ratelimit())
  560. printk(KERN_WARNING "%s: packet dropped\n",
  561. dev->name);
  562. if (unlikely(netif_msg_hw(priv)))
  563. cpmac_dump_desc(dev, desc);
  564. desc->dataflags = CPMAC_OWN;
  565. dev->stats.rx_dropped++;
  566. }
  567. desc = desc->next;
  568. }
  569. }
  570. static void cpmac_clear_tx(struct net_device *dev)
  571. {
  572. struct cpmac_priv *priv = netdev_priv(dev);
  573. int i;
  574. if (unlikely(!priv->desc_ring))
  575. return;
  576. for (i = 0; i < CPMAC_QUEUES; i++) {
  577. priv->desc_ring[i].dataflags = 0;
  578. if (priv->desc_ring[i].skb) {
  579. dev_kfree_skb_any(priv->desc_ring[i].skb);
  580. if (netif_subqueue_stopped(dev, i))
  581. netif_wake_subqueue(dev, i);
  582. }
  583. }
  584. }
  585. static void cpmac_hw_error(struct work_struct *work)
  586. {
  587. struct cpmac_priv *priv =
  588. container_of(work, struct cpmac_priv, reset_work);
  589. spin_lock(&priv->rx_lock);
  590. cpmac_clear_rx(priv->dev);
  591. spin_unlock(&priv->rx_lock);
  592. cpmac_clear_tx(priv->dev);
  593. cpmac_hw_start(priv->dev);
  594. napi_enable(&priv->napi);
  595. netif_start_queue(priv->dev);
  596. }
  597. static irqreturn_t cpmac_irq(int irq, void *dev_id)
  598. {
  599. struct net_device *dev = dev_id;
  600. struct cpmac_priv *priv;
  601. int queue;
  602. u32 status;
  603. priv = netdev_priv(dev);
  604. status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
  605. if (unlikely(netif_msg_intr(priv)))
  606. printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
  607. status);
  608. if (status & MAC_INT_TX)
  609. cpmac_end_xmit(dev, (status & 7));
  610. if (status & MAC_INT_RX) {
  611. queue = (status >> 8) & 7;
  612. if (netif_rx_schedule_prep(dev, &priv->napi)) {
  613. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
  614. __netif_rx_schedule(dev, &priv->napi);
  615. }
  616. }
  617. cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
  618. if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS))) {
  619. if (netif_msg_drv(priv) && net_ratelimit())
  620. printk(KERN_ERR "%s: hw error, resetting...\n",
  621. dev->name);
  622. netif_stop_queue(dev);
  623. napi_disable(&priv->napi);
  624. cpmac_hw_stop(dev);
  625. schedule_work(&priv->reset_work);
  626. if (unlikely(netif_msg_hw(priv)))
  627. cpmac_dump_regs(dev);
  628. }
  629. return IRQ_HANDLED;
  630. }
  631. static void cpmac_tx_timeout(struct net_device *dev)
  632. {
  633. struct cpmac_priv *priv = netdev_priv(dev);
  634. int i;
  635. spin_lock(&priv->lock);
  636. dev->stats.tx_errors++;
  637. spin_unlock(&priv->lock);
  638. if (netif_msg_tx_err(priv) && net_ratelimit())
  639. printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
  640. /*
  641. * FIXME: waking up random queue is not the best thing to
  642. * do... on the other hand why we got here at all?
  643. */
  644. #ifdef CONFIG_NETDEVICES_MULTIQUEUE
  645. for (i = 0; i < CPMAC_QUEUES; i++)
  646. if (priv->desc_ring[i].skb) {
  647. priv->desc_ring[i].dataflags = 0;
  648. dev_kfree_skb_any(priv->desc_ring[i].skb);
  649. netif_wake_subqueue(dev, i);
  650. break;
  651. }
  652. #else
  653. priv->desc_ring[0].dataflags = 0;
  654. if (priv->desc_ring[0].skb)
  655. dev_kfree_skb_any(priv->desc_ring[0].skb);
  656. netif_wake_queue(dev);
  657. #endif
  658. }
  659. static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  660. {
  661. struct cpmac_priv *priv = netdev_priv(dev);
  662. if (!(netif_running(dev)))
  663. return -EINVAL;
  664. if (!priv->phy)
  665. return -EINVAL;
  666. if ((cmd == SIOCGMIIPHY) || (cmd == SIOCGMIIREG) ||
  667. (cmd == SIOCSMIIREG))
  668. return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
  669. return -EOPNOTSUPP;
  670. }
  671. static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  672. {
  673. struct cpmac_priv *priv = netdev_priv(dev);
  674. if (priv->phy)
  675. return phy_ethtool_gset(priv->phy, cmd);
  676. return -EINVAL;
  677. }
  678. static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  679. {
  680. struct cpmac_priv *priv = netdev_priv(dev);
  681. if (!capable(CAP_NET_ADMIN))
  682. return -EPERM;
  683. if (priv->phy)
  684. return phy_ethtool_sset(priv->phy, cmd);
  685. return -EINVAL;
  686. }
  687. static void cpmac_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
  688. {
  689. struct cpmac_priv *priv = netdev_priv(dev);
  690. ring->rx_max_pending = 1024;
  691. ring->rx_mini_max_pending = 1;
  692. ring->rx_jumbo_max_pending = 1;
  693. ring->tx_max_pending = 1;
  694. ring->rx_pending = priv->ring_size;
  695. ring->rx_mini_pending = 1;
  696. ring->rx_jumbo_pending = 1;
  697. ring->tx_pending = 1;
  698. }
  699. static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
  700. {
  701. struct cpmac_priv *priv = netdev_priv(dev);
  702. if (netif_running(dev))
  703. return -EBUSY;
  704. priv->ring_size = ring->rx_pending;
  705. return 0;
  706. }
  707. static void cpmac_get_drvinfo(struct net_device *dev,
  708. struct ethtool_drvinfo *info)
  709. {
  710. strcpy(info->driver, "cpmac");
  711. strcpy(info->version, CPMAC_VERSION);
  712. info->fw_version[0] = '\0';
  713. sprintf(info->bus_info, "%s", "cpmac");
  714. info->regdump_len = 0;
  715. }
  716. static const struct ethtool_ops cpmac_ethtool_ops = {
  717. .get_settings = cpmac_get_settings,
  718. .set_settings = cpmac_set_settings,
  719. .get_drvinfo = cpmac_get_drvinfo,
  720. .get_link = ethtool_op_get_link,
  721. .get_ringparam = cpmac_get_ringparam,
  722. .set_ringparam = cpmac_set_ringparam,
  723. };
  724. static void cpmac_adjust_link(struct net_device *dev)
  725. {
  726. struct cpmac_priv *priv = netdev_priv(dev);
  727. int new_state = 0;
  728. spin_lock(&priv->lock);
  729. if (priv->phy->link) {
  730. netif_start_queue(dev);
  731. if (priv->phy->duplex != priv->oldduplex) {
  732. new_state = 1;
  733. priv->oldduplex = priv->phy->duplex;
  734. }
  735. if (priv->phy->speed != priv->oldspeed) {
  736. new_state = 1;
  737. priv->oldspeed = priv->phy->speed;
  738. }
  739. if (!priv->oldlink) {
  740. new_state = 1;
  741. priv->oldlink = 1;
  742. netif_schedule(dev);
  743. }
  744. } else if (priv->oldlink) {
  745. netif_stop_queue(dev);
  746. new_state = 1;
  747. priv->oldlink = 0;
  748. priv->oldspeed = 0;
  749. priv->oldduplex = -1;
  750. }
  751. if (new_state && netif_msg_link(priv) && net_ratelimit())
  752. phy_print_status(priv->phy);
  753. spin_unlock(&priv->lock);
  754. }
  755. static int cpmac_open(struct net_device *dev)
  756. {
  757. int i, size, res;
  758. struct cpmac_priv *priv = netdev_priv(dev);
  759. struct resource *mem;
  760. struct cpmac_desc *desc;
  761. struct sk_buff *skb;
  762. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  763. if (!request_mem_region(mem->start, mem->end - mem->start, dev->name)) {
  764. if (netif_msg_drv(priv))
  765. printk(KERN_ERR "%s: failed to request registers\n",
  766. dev->name);
  767. res = -ENXIO;
  768. goto fail_reserve;
  769. }
  770. priv->regs = ioremap(mem->start, mem->end - mem->start);
  771. if (!priv->regs) {
  772. if (netif_msg_drv(priv))
  773. printk(KERN_ERR "%s: failed to remap registers\n",
  774. dev->name);
  775. res = -ENXIO;
  776. goto fail_remap;
  777. }
  778. size = priv->ring_size + CPMAC_QUEUES;
  779. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  780. sizeof(struct cpmac_desc) * size,
  781. &priv->dma_ring,
  782. GFP_KERNEL);
  783. if (!priv->desc_ring) {
  784. res = -ENOMEM;
  785. goto fail_alloc;
  786. }
  787. for (i = 0; i < size; i++)
  788. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  789. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  790. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  791. skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
  792. if (unlikely(!skb)) {
  793. res = -ENOMEM;
  794. goto fail_desc;
  795. }
  796. skb_reserve(skb, 2);
  797. desc->skb = skb;
  798. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  799. CPMAC_SKB_SIZE,
  800. DMA_FROM_DEVICE);
  801. desc->hw_data = (u32)desc->data_mapping;
  802. desc->buflen = CPMAC_SKB_SIZE;
  803. desc->dataflags = CPMAC_OWN;
  804. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  805. desc->hw_next = (u32)desc->next->mapping;
  806. }
  807. if ((res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED,
  808. dev->name, dev))) {
  809. if (netif_msg_drv(priv))
  810. printk(KERN_ERR "%s: failed to obtain irq\n",
  811. dev->name);
  812. goto fail_irq;
  813. }
  814. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  815. cpmac_hw_start(dev);
  816. napi_enable(&priv->napi);
  817. priv->phy->state = PHY_CHANGELINK;
  818. phy_start(priv->phy);
  819. return 0;
  820. fail_irq:
  821. fail_desc:
  822. for (i = 0; i < priv->ring_size; i++) {
  823. if (priv->rx_head[i].skb) {
  824. dma_unmap_single(&dev->dev,
  825. priv->rx_head[i].data_mapping,
  826. CPMAC_SKB_SIZE,
  827. DMA_FROM_DEVICE);
  828. kfree_skb(priv->rx_head[i].skb);
  829. }
  830. }
  831. fail_alloc:
  832. kfree(priv->desc_ring);
  833. iounmap(priv->regs);
  834. fail_remap:
  835. release_mem_region(mem->start, mem->end - mem->start);
  836. fail_reserve:
  837. return res;
  838. }
  839. static int cpmac_stop(struct net_device *dev)
  840. {
  841. int i;
  842. struct cpmac_priv *priv = netdev_priv(dev);
  843. struct resource *mem;
  844. netif_stop_queue(dev);
  845. cancel_work_sync(&priv->reset_work);
  846. napi_disable(&priv->napi);
  847. phy_stop(priv->phy);
  848. cpmac_hw_stop(dev);
  849. for (i = 0; i < 8; i++)
  850. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  851. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  852. cpmac_write(priv->regs, CPMAC_MBP, 0);
  853. free_irq(dev->irq, dev);
  854. iounmap(priv->regs);
  855. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  856. release_mem_region(mem->start, mem->end - mem->start);
  857. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  858. for (i = 0; i < priv->ring_size; i++) {
  859. if (priv->rx_head[i].skb) {
  860. dma_unmap_single(&dev->dev,
  861. priv->rx_head[i].data_mapping,
  862. CPMAC_SKB_SIZE,
  863. DMA_FROM_DEVICE);
  864. kfree_skb(priv->rx_head[i].skb);
  865. }
  866. }
  867. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  868. (CPMAC_QUEUES + priv->ring_size),
  869. priv->desc_ring, priv->dma_ring);
  870. return 0;
  871. }
  872. static int external_switch;
  873. static int __devinit cpmac_probe(struct platform_device *pdev)
  874. {
  875. int rc, phy_id, i;
  876. char *mdio_bus_id = "0";
  877. struct resource *mem;
  878. struct cpmac_priv *priv;
  879. struct net_device *dev;
  880. struct plat_cpmac_data *pdata;
  881. DECLARE_MAC_BUF(mac);
  882. pdata = pdev->dev.platform_data;
  883. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  884. if (!(pdata->phy_mask & (1 << phy_id)))
  885. continue;
  886. if (!cpmac_mii.phy_map[phy_id])
  887. continue;
  888. break;
  889. }
  890. if (phy_id == PHY_MAX_ADDR) {
  891. if (external_switch || dumb_switch) {
  892. struct fixed_phy_status status = {};
  893. /*
  894. * FIXME: this should be in the platform code!
  895. * Since there is not platform code at all (that is,
  896. * no mainline users of that driver), place it here
  897. * for now.
  898. */
  899. phy_id = 0;
  900. status.link = 1;
  901. status.duplex = 1;
  902. status.speed = 100;
  903. fixed_phy_add(PHY_POLL, phy_id, &status);
  904. } else {
  905. printk(KERN_ERR "cpmac: no PHY present\n");
  906. return -ENODEV;
  907. }
  908. }
  909. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  910. if (!dev) {
  911. printk(KERN_ERR "cpmac: Unable to allocate net_device\n");
  912. return -ENOMEM;
  913. }
  914. platform_set_drvdata(pdev, dev);
  915. priv = netdev_priv(dev);
  916. priv->pdev = pdev;
  917. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  918. if (!mem) {
  919. rc = -ENODEV;
  920. goto fail;
  921. }
  922. dev->irq = platform_get_irq_byname(pdev, "irq");
  923. dev->open = cpmac_open;
  924. dev->stop = cpmac_stop;
  925. dev->set_config = cpmac_config;
  926. dev->hard_start_xmit = cpmac_start_xmit;
  927. dev->do_ioctl = cpmac_ioctl;
  928. dev->set_multicast_list = cpmac_set_multicast_list;
  929. dev->tx_timeout = cpmac_tx_timeout;
  930. dev->ethtool_ops = &cpmac_ethtool_ops;
  931. dev->features |= NETIF_F_MULTI_QUEUE;
  932. netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
  933. spin_lock_init(&priv->lock);
  934. spin_lock_init(&priv->rx_lock);
  935. priv->dev = dev;
  936. priv->ring_size = 64;
  937. priv->msg_enable = netif_msg_init(debug_level, 0xff);
  938. memcpy(dev->dev_addr, pdata->dev_addr, sizeof(dev->dev_addr));
  939. snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
  940. priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0,
  941. PHY_INTERFACE_MODE_MII);
  942. if (IS_ERR(priv->phy)) {
  943. if (netif_msg_drv(priv))
  944. printk(KERN_ERR "%s: Could not attach to PHY\n",
  945. dev->name);
  946. return PTR_ERR(priv->phy);
  947. }
  948. if ((rc = register_netdev(dev))) {
  949. printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
  950. dev->name);
  951. goto fail;
  952. }
  953. if (netif_msg_probe(priv)) {
  954. printk(KERN_INFO
  955. "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
  956. "mac: %s)\n", dev->name, (void *)mem->start, dev->irq,
  957. priv->phy_name, print_mac(mac, dev->dev_addr));
  958. }
  959. return 0;
  960. fail:
  961. free_netdev(dev);
  962. return rc;
  963. }
  964. static int __devexit cpmac_remove(struct platform_device *pdev)
  965. {
  966. struct net_device *dev = platform_get_drvdata(pdev);
  967. unregister_netdev(dev);
  968. free_netdev(dev);
  969. return 0;
  970. }
  971. static struct platform_driver cpmac_driver = {
  972. .driver.name = "cpmac",
  973. .driver.owner = THIS_MODULE,
  974. .probe = cpmac_probe,
  975. .remove = __devexit_p(cpmac_remove),
  976. };
  977. int __devinit cpmac_init(void)
  978. {
  979. u32 mask;
  980. int i, res;
  981. cpmac_mii.priv = ioremap(AR7_REGS_MDIO, 256);
  982. if (!cpmac_mii.priv) {
  983. printk(KERN_ERR "Can't ioremap mdio registers\n");
  984. return -ENXIO;
  985. }
  986. #warning FIXME: unhardcode gpio&reset bits
  987. ar7_gpio_disable(26);
  988. ar7_gpio_disable(27);
  989. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  990. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  991. ar7_device_reset(AR7_RESET_BIT_EPHY);
  992. cpmac_mii.reset(&cpmac_mii);
  993. for (i = 0; i < 300000; i++)
  994. if ((mask = cpmac_read(cpmac_mii.priv, CPMAC_MDIO_ALIVE)))
  995. break;
  996. else
  997. cpu_relax();
  998. mask &= 0x7fffffff;
  999. if (mask & (mask - 1)) {
  1000. external_switch = 1;
  1001. mask = 0;
  1002. }
  1003. cpmac_mii.phy_mask = ~(mask | 0x80000000);
  1004. snprintf(cpmac_mii.id, MII_BUS_ID_SIZE, "0");
  1005. res = mdiobus_register(&cpmac_mii);
  1006. if (res)
  1007. goto fail_mii;
  1008. res = platform_driver_register(&cpmac_driver);
  1009. if (res)
  1010. goto fail_cpmac;
  1011. return 0;
  1012. fail_cpmac:
  1013. mdiobus_unregister(&cpmac_mii);
  1014. fail_mii:
  1015. iounmap(cpmac_mii.priv);
  1016. return res;
  1017. }
  1018. void __devexit cpmac_exit(void)
  1019. {
  1020. platform_driver_unregister(&cpmac_driver);
  1021. mdiobus_unregister(&cpmac_mii);
  1022. iounmap(cpmac_mii.priv);
  1023. }
  1024. module_init(cpmac_init);
  1025. module_exit(cpmac_exit);