cpmac.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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->queue_mapping;
  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. if (!dev)
  603. return IRQ_NONE;
  604. priv = netdev_priv(dev);
  605. status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
  606. if (unlikely(netif_msg_intr(priv)))
  607. printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
  608. status);
  609. if (status & MAC_INT_TX)
  610. cpmac_end_xmit(dev, (status & 7));
  611. if (status & MAC_INT_RX) {
  612. queue = (status >> 8) & 7;
  613. if (netif_rx_schedule_prep(dev, &priv->napi)) {
  614. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
  615. __netif_rx_schedule(dev, &priv->napi);
  616. }
  617. }
  618. cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
  619. if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS))) {
  620. if (netif_msg_drv(priv) && net_ratelimit())
  621. printk(KERN_ERR "%s: hw error, resetting...\n",
  622. dev->name);
  623. netif_stop_queue(dev);
  624. napi_disable(&priv->napi);
  625. cpmac_hw_stop(dev);
  626. schedule_work(&priv->reset_work);
  627. if (unlikely(netif_msg_hw(priv)))
  628. cpmac_dump_regs(dev);
  629. }
  630. return IRQ_HANDLED;
  631. }
  632. static void cpmac_tx_timeout(struct net_device *dev)
  633. {
  634. struct cpmac_priv *priv = netdev_priv(dev);
  635. int i;
  636. spin_lock(&priv->lock);
  637. dev->stats.tx_errors++;
  638. spin_unlock(&priv->lock);
  639. if (netif_msg_tx_err(priv) && net_ratelimit())
  640. printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
  641. /*
  642. * FIXME: waking up random queue is not the best thing to
  643. * do... on the other hand why we got here at all?
  644. */
  645. #ifdef CONFIG_NETDEVICES_MULTIQUEUE
  646. for (i = 0; i < CPMAC_QUEUES; i++)
  647. if (priv->desc_ring[i].skb) {
  648. priv->desc_ring[i].dataflags = 0;
  649. dev_kfree_skb_any(priv->desc_ring[i].skb);
  650. netif_wake_subqueue(dev, i);
  651. break;
  652. }
  653. #else
  654. priv->desc_ring[0].dataflags = 0;
  655. if (priv->desc_ring[0].skb)
  656. dev_kfree_skb_any(priv->desc_ring[0].skb);
  657. netif_wake_queue(dev);
  658. #endif
  659. }
  660. static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  661. {
  662. struct cpmac_priv *priv = netdev_priv(dev);
  663. if (!(netif_running(dev)))
  664. return -EINVAL;
  665. if (!priv->phy)
  666. return -EINVAL;
  667. if ((cmd == SIOCGMIIPHY) || (cmd == SIOCGMIIREG) ||
  668. (cmd == SIOCSMIIREG))
  669. return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
  670. return -EOPNOTSUPP;
  671. }
  672. static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  673. {
  674. struct cpmac_priv *priv = netdev_priv(dev);
  675. if (priv->phy)
  676. return phy_ethtool_gset(priv->phy, cmd);
  677. return -EINVAL;
  678. }
  679. static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  680. {
  681. struct cpmac_priv *priv = netdev_priv(dev);
  682. if (!capable(CAP_NET_ADMIN))
  683. return -EPERM;
  684. if (priv->phy)
  685. return phy_ethtool_sset(priv->phy, cmd);
  686. return -EINVAL;
  687. }
  688. static void cpmac_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
  689. {
  690. struct cpmac_priv *priv = netdev_priv(dev);
  691. ring->rx_max_pending = 1024;
  692. ring->rx_mini_max_pending = 1;
  693. ring->rx_jumbo_max_pending = 1;
  694. ring->tx_max_pending = 1;
  695. ring->rx_pending = priv->ring_size;
  696. ring->rx_mini_pending = 1;
  697. ring->rx_jumbo_pending = 1;
  698. ring->tx_pending = 1;
  699. }
  700. static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
  701. {
  702. struct cpmac_priv *priv = netdev_priv(dev);
  703. if (netif_running(dev))
  704. return -EBUSY;
  705. priv->ring_size = ring->rx_pending;
  706. return 0;
  707. }
  708. static void cpmac_get_drvinfo(struct net_device *dev,
  709. struct ethtool_drvinfo *info)
  710. {
  711. strcpy(info->driver, "cpmac");
  712. strcpy(info->version, CPMAC_VERSION);
  713. info->fw_version[0] = '\0';
  714. sprintf(info->bus_info, "%s", "cpmac");
  715. info->regdump_len = 0;
  716. }
  717. static const struct ethtool_ops cpmac_ethtool_ops = {
  718. .get_settings = cpmac_get_settings,
  719. .set_settings = cpmac_set_settings,
  720. .get_drvinfo = cpmac_get_drvinfo,
  721. .get_link = ethtool_op_get_link,
  722. .get_ringparam = cpmac_get_ringparam,
  723. .set_ringparam = cpmac_set_ringparam,
  724. };
  725. static void cpmac_adjust_link(struct net_device *dev)
  726. {
  727. struct cpmac_priv *priv = netdev_priv(dev);
  728. int new_state = 0;
  729. spin_lock(&priv->lock);
  730. if (priv->phy->link) {
  731. netif_start_queue(dev);
  732. if (priv->phy->duplex != priv->oldduplex) {
  733. new_state = 1;
  734. priv->oldduplex = priv->phy->duplex;
  735. }
  736. if (priv->phy->speed != priv->oldspeed) {
  737. new_state = 1;
  738. priv->oldspeed = priv->phy->speed;
  739. }
  740. if (!priv->oldlink) {
  741. new_state = 1;
  742. priv->oldlink = 1;
  743. netif_schedule(dev);
  744. }
  745. } else if (priv->oldlink) {
  746. netif_stop_queue(dev);
  747. new_state = 1;
  748. priv->oldlink = 0;
  749. priv->oldspeed = 0;
  750. priv->oldduplex = -1;
  751. }
  752. if (new_state && netif_msg_link(priv) && net_ratelimit())
  753. phy_print_status(priv->phy);
  754. spin_unlock(&priv->lock);
  755. }
  756. static int cpmac_link_update(struct net_device *dev,
  757. struct fixed_phy_status *status)
  758. {
  759. status->link = 1;
  760. status->speed = 100;
  761. status->duplex = 1;
  762. return 0;
  763. }
  764. static int cpmac_open(struct net_device *dev)
  765. {
  766. int i, size, res;
  767. struct cpmac_priv *priv = netdev_priv(dev);
  768. struct resource *mem;
  769. struct cpmac_desc *desc;
  770. struct sk_buff *skb;
  771. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  772. if (!request_mem_region(mem->start, mem->end - mem->start, dev->name)) {
  773. if (netif_msg_drv(priv))
  774. printk(KERN_ERR "%s: failed to request registers\n",
  775. dev->name);
  776. res = -ENXIO;
  777. goto fail_reserve;
  778. }
  779. priv->regs = ioremap(mem->start, mem->end - mem->start);
  780. if (!priv->regs) {
  781. if (netif_msg_drv(priv))
  782. printk(KERN_ERR "%s: failed to remap registers\n",
  783. dev->name);
  784. res = -ENXIO;
  785. goto fail_remap;
  786. }
  787. size = priv->ring_size + CPMAC_QUEUES;
  788. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  789. sizeof(struct cpmac_desc) * size,
  790. &priv->dma_ring,
  791. GFP_KERNEL);
  792. if (!priv->desc_ring) {
  793. res = -ENOMEM;
  794. goto fail_alloc;
  795. }
  796. for (i = 0; i < size; i++)
  797. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  798. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  799. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  800. skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
  801. if (unlikely(!skb)) {
  802. res = -ENOMEM;
  803. goto fail_desc;
  804. }
  805. skb_reserve(skb, 2);
  806. desc->skb = skb;
  807. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  808. CPMAC_SKB_SIZE,
  809. DMA_FROM_DEVICE);
  810. desc->hw_data = (u32)desc->data_mapping;
  811. desc->buflen = CPMAC_SKB_SIZE;
  812. desc->dataflags = CPMAC_OWN;
  813. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  814. desc->hw_next = (u32)desc->next->mapping;
  815. }
  816. if ((res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED,
  817. dev->name, dev))) {
  818. if (netif_msg_drv(priv))
  819. printk(KERN_ERR "%s: failed to obtain irq\n",
  820. dev->name);
  821. goto fail_irq;
  822. }
  823. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  824. cpmac_hw_start(dev);
  825. napi_enable(&priv->napi);
  826. priv->phy->state = PHY_CHANGELINK;
  827. phy_start(priv->phy);
  828. return 0;
  829. fail_irq:
  830. fail_desc:
  831. for (i = 0; i < priv->ring_size; i++) {
  832. if (priv->rx_head[i].skb) {
  833. dma_unmap_single(&dev->dev,
  834. priv->rx_head[i].data_mapping,
  835. CPMAC_SKB_SIZE,
  836. DMA_FROM_DEVICE);
  837. kfree_skb(priv->rx_head[i].skb);
  838. }
  839. }
  840. fail_alloc:
  841. kfree(priv->desc_ring);
  842. iounmap(priv->regs);
  843. fail_remap:
  844. release_mem_region(mem->start, mem->end - mem->start);
  845. fail_reserve:
  846. return res;
  847. }
  848. static int cpmac_stop(struct net_device *dev)
  849. {
  850. int i;
  851. struct cpmac_priv *priv = netdev_priv(dev);
  852. struct resource *mem;
  853. netif_stop_queue(dev);
  854. cancel_work_sync(&priv->reset_work);
  855. napi_disable(&priv->napi);
  856. phy_stop(priv->phy);
  857. cpmac_hw_stop(dev);
  858. for (i = 0; i < 8; i++)
  859. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  860. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  861. cpmac_write(priv->regs, CPMAC_MBP, 0);
  862. free_irq(dev->irq, dev);
  863. iounmap(priv->regs);
  864. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  865. release_mem_region(mem->start, mem->end - mem->start);
  866. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  867. for (i = 0; i < priv->ring_size; i++) {
  868. if (priv->rx_head[i].skb) {
  869. dma_unmap_single(&dev->dev,
  870. priv->rx_head[i].data_mapping,
  871. CPMAC_SKB_SIZE,
  872. DMA_FROM_DEVICE);
  873. kfree_skb(priv->rx_head[i].skb);
  874. }
  875. }
  876. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  877. (CPMAC_QUEUES + priv->ring_size),
  878. priv->desc_ring, priv->dma_ring);
  879. return 0;
  880. }
  881. static int external_switch;
  882. static int __devinit cpmac_probe(struct platform_device *pdev)
  883. {
  884. int rc, phy_id, i;
  885. struct resource *mem;
  886. struct cpmac_priv *priv;
  887. struct net_device *dev;
  888. struct plat_cpmac_data *pdata;
  889. struct fixed_info *fixed_phy;
  890. DECLARE_MAC_BUF(mac);
  891. pdata = pdev->dev.platform_data;
  892. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  893. if (!(pdata->phy_mask & (1 << phy_id)))
  894. continue;
  895. if (!cpmac_mii.phy_map[phy_id])
  896. continue;
  897. break;
  898. }
  899. if (phy_id == PHY_MAX_ADDR) {
  900. if (external_switch || dumb_switch)
  901. phy_id = 0;
  902. else {
  903. printk(KERN_ERR "cpmac: no PHY present\n");
  904. return -ENODEV;
  905. }
  906. }
  907. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  908. if (!dev) {
  909. printk(KERN_ERR "cpmac: Unable to allocate net_device\n");
  910. return -ENOMEM;
  911. }
  912. platform_set_drvdata(pdev, dev);
  913. priv = netdev_priv(dev);
  914. priv->pdev = pdev;
  915. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  916. if (!mem) {
  917. rc = -ENODEV;
  918. goto fail;
  919. }
  920. dev->irq = platform_get_irq_byname(pdev, "irq");
  921. dev->open = cpmac_open;
  922. dev->stop = cpmac_stop;
  923. dev->set_config = cpmac_config;
  924. dev->hard_start_xmit = cpmac_start_xmit;
  925. dev->do_ioctl = cpmac_ioctl;
  926. dev->set_multicast_list = cpmac_set_multicast_list;
  927. dev->tx_timeout = cpmac_tx_timeout;
  928. dev->ethtool_ops = &cpmac_ethtool_ops;
  929. dev->features |= NETIF_F_MULTI_QUEUE;
  930. netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
  931. spin_lock_init(&priv->lock);
  932. spin_lock_init(&priv->rx_lock);
  933. priv->dev = dev;
  934. priv->ring_size = 64;
  935. priv->msg_enable = netif_msg_init(debug_level, 0xff);
  936. memcpy(dev->dev_addr, pdata->dev_addr, sizeof(dev->dev_addr));
  937. if (phy_id == 31) {
  938. snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT, cpmac_mii.id,
  939. phy_id);
  940. } else {
  941. /* Let's try to get a free fixed phy... */
  942. for (i = 0; i < MAX_PHY_AMNT; i++) {
  943. fixed_phy = fixed_mdio_get_phydev(i);
  944. if (!fixed_phy)
  945. continue;
  946. if (!fixed_phy->phydev->attached_dev) {
  947. strncpy(priv->phy_name,
  948. fixed_phy->phydev->dev.bus_id,
  949. BUS_ID_SIZE);
  950. fixed_mdio_set_link_update(fixed_phy->phydev,
  951. &cpmac_link_update);
  952. goto phy_found;
  953. }
  954. }
  955. if (netif_msg_drv(priv))
  956. printk(KERN_ERR "%s: Could not find fixed PHY\n",
  957. dev->name);
  958. rc = -ENODEV;
  959. goto fail;
  960. }
  961. phy_found:
  962. priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0,
  963. PHY_INTERFACE_MODE_MII);
  964. if (IS_ERR(priv->phy)) {
  965. if (netif_msg_drv(priv))
  966. printk(KERN_ERR "%s: Could not attach to PHY\n",
  967. dev->name);
  968. return PTR_ERR(priv->phy);
  969. }
  970. if ((rc = register_netdev(dev))) {
  971. printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
  972. dev->name);
  973. goto fail;
  974. }
  975. if (netif_msg_probe(priv)) {
  976. printk(KERN_INFO
  977. "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
  978. "mac: %s)\n", dev->name, (void *)mem->start, dev->irq,
  979. priv->phy_name, print_mac(mac, dev->dev_addr));
  980. }
  981. return 0;
  982. fail:
  983. free_netdev(dev);
  984. return rc;
  985. }
  986. static int __devexit cpmac_remove(struct platform_device *pdev)
  987. {
  988. struct net_device *dev = platform_get_drvdata(pdev);
  989. unregister_netdev(dev);
  990. free_netdev(dev);
  991. return 0;
  992. }
  993. static struct platform_driver cpmac_driver = {
  994. .driver.name = "cpmac",
  995. .probe = cpmac_probe,
  996. .remove = __devexit_p(cpmac_remove),
  997. };
  998. int __devinit cpmac_init(void)
  999. {
  1000. u32 mask;
  1001. int i, res;
  1002. cpmac_mii.priv = ioremap(AR7_REGS_MDIO, 256);
  1003. if (!cpmac_mii.priv) {
  1004. printk(KERN_ERR "Can't ioremap mdio registers\n");
  1005. return -ENXIO;
  1006. }
  1007. #warning FIXME: unhardcode gpio&reset bits
  1008. ar7_gpio_disable(26);
  1009. ar7_gpio_disable(27);
  1010. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  1011. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  1012. ar7_device_reset(AR7_RESET_BIT_EPHY);
  1013. cpmac_mii.reset(&cpmac_mii);
  1014. for (i = 0; i < 300000; i++)
  1015. if ((mask = cpmac_read(cpmac_mii.priv, CPMAC_MDIO_ALIVE)))
  1016. break;
  1017. else
  1018. cpu_relax();
  1019. mask &= 0x7fffffff;
  1020. if (mask & (mask - 1)) {
  1021. external_switch = 1;
  1022. mask = 0;
  1023. }
  1024. cpmac_mii.phy_mask = ~(mask | 0x80000000);
  1025. res = mdiobus_register(&cpmac_mii);
  1026. if (res)
  1027. goto fail_mii;
  1028. res = platform_driver_register(&cpmac_driver);
  1029. if (res)
  1030. goto fail_cpmac;
  1031. return 0;
  1032. fail_cpmac:
  1033. mdiobus_unregister(&cpmac_mii);
  1034. fail_mii:
  1035. iounmap(cpmac_mii.priv);
  1036. return res;
  1037. }
  1038. void __devexit cpmac_exit(void)
  1039. {
  1040. platform_driver_unregister(&cpmac_driver);
  1041. mdiobus_unregister(&cpmac_mii);
  1042. iounmap(cpmac_mii.priv);
  1043. }
  1044. module_init(cpmac_init);
  1045. module_exit(cpmac_exit);