cpmac.c 31 KB

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