cpmac.c 32 KB

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