110-ar2313_ethernet.patch 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. --- a/drivers/net/ethernet/atheros/Makefile
  2. +++ b/drivers/net/ethernet/atheros/Makefile
  3. @@ -9,3 +9,4 @@ obj-$(CONFIG_ATL2) += atlx/
  4. obj-$(CONFIG_ATL1E) += atl1e/
  5. obj-$(CONFIG_ATL1C) += atl1c/
  6. obj-$(CONFIG_ALX) += alx/
  7. +obj-$(CONFIG_NET_AR231X) += ar231x/
  8. --- a/drivers/net/ethernet/atheros/Kconfig
  9. +++ b/drivers/net/ethernet/atheros/Kconfig
  10. @@ -6,7 +6,7 @@
  11. config NET_VENDOR_ATHEROS
  12. bool "Atheros devices"
  13. default y
  14. - depends on (PCI || ATH79)
  15. + depends on (PCI || ATH25 || ATH79)
  16. help
  17. If you have a network (Ethernet) card belonging to this class, say Y.
  18. @@ -88,4 +88,10 @@ config ALX
  19. To compile this driver as a module, choose M here. The module
  20. will be called alx.
  21. +config NET_AR231X
  22. + tristate "Atheros AR231X built-in Ethernet support"
  23. + depends on ATH25
  24. + help
  25. + Support for the AR231x/531x ethernet controller
  26. +
  27. endif # NET_VENDOR_ATHEROS
  28. --- /dev/null
  29. +++ b/drivers/net/ethernet/atheros/ar231x/Makefile
  30. @@ -0,0 +1 @@
  31. +obj-$(CONFIG_NET_AR231X) += ar231x.o
  32. --- /dev/null
  33. +++ b/drivers/net/ethernet/atheros/ar231x/ar231x.c
  34. @@ -0,0 +1,1117 @@
  35. +/*
  36. + * ar231x.c: Linux driver for the Atheros AR231x Ethernet device.
  37. + *
  38. + * Copyright (C) 2004 by Sameer Dekate <[email protected]>
  39. + * Copyright (C) 2006 Imre Kaloz <[email protected]>
  40. + * Copyright (C) 2006-2009 Felix Fietkau <[email protected]>
  41. + *
  42. + * Thanks to Atheros for providing hardware and documentation
  43. + * enabling me to write this driver.
  44. + *
  45. + * This program is free software; you can redistribute it and/or modify
  46. + * it under the terms of the GNU General Public License as published by
  47. + * the Free Software Foundation; either version 2 of the License, or
  48. + * (at your option) any later version.
  49. + *
  50. + * Additional credits:
  51. + * This code is taken from John Taylor's Sibyte driver and then
  52. + * modified for the AR2313.
  53. + */
  54. +
  55. +#include <linux/module.h>
  56. +#include <linux/types.h>
  57. +#include <linux/errno.h>
  58. +#include <linux/ioport.h>
  59. +#include <linux/netdevice.h>
  60. +#include <linux/etherdevice.h>
  61. +#include <linux/interrupt.h>
  62. +#include <linux/skbuff.h>
  63. +#include <linux/init.h>
  64. +#include <linux/delay.h>
  65. +#include <linux/mm.h>
  66. +#include <linux/mii.h>
  67. +#include <linux/phy.h>
  68. +#include <linux/platform_device.h>
  69. +#include <linux/io.h>
  70. +
  71. +#define AR2313_MTU 1692
  72. +#define AR2313_PRIOS 1
  73. +#define AR2313_QUEUES (2*AR2313_PRIOS)
  74. +#define AR2313_DESCR_ENTRIES 64
  75. +
  76. +#ifndef min
  77. +#define min(a, b) (((a) < (b)) ? (a) : (b))
  78. +#endif
  79. +
  80. +#ifndef SMP_CACHE_BYTES
  81. +#define SMP_CACHE_BYTES L1_CACHE_BYTES
  82. +#endif
  83. +
  84. +#define AR2313_MBOX_SET_BIT 0x8
  85. +
  86. +#include "ar231x.h"
  87. +
  88. +/**
  89. + * New interrupt handler strategy:
  90. + *
  91. + * An old interrupt handler worked using the traditional method of
  92. + * replacing an skbuff with a new one when a packet arrives. However
  93. + * the rx rings do not need to contain a static number of buffer
  94. + * descriptors, thus it makes sense to move the memory allocation out
  95. + * of the main interrupt handler and do it in a bottom half handler
  96. + * and only allocate new buffers when the number of buffers in the
  97. + * ring is below a certain threshold. In order to avoid starving the
  98. + * NIC under heavy load it is however necessary to force allocation
  99. + * when hitting a minimum threshold. The strategy for alloction is as
  100. + * follows:
  101. + *
  102. + * RX_LOW_BUF_THRES - allocate buffers in the bottom half
  103. + * RX_PANIC_LOW_THRES - we are very low on buffers, allocate
  104. + * the buffers in the interrupt handler
  105. + * RX_RING_THRES - maximum number of buffers in the rx ring
  106. + *
  107. + * One advantagous side effect of this allocation approach is that the
  108. + * entire rx processing can be done without holding any spin lock
  109. + * since the rx rings and registers are totally independent of the tx
  110. + * ring and its registers. This of course includes the kmalloc's of
  111. + * new skb's. Thus start_xmit can run in parallel with rx processing
  112. + * and the memory allocation on SMP systems.
  113. + *
  114. + * Note that running the skb reallocation in a bottom half opens up
  115. + * another can of races which needs to be handled properly. In
  116. + * particular it can happen that the interrupt handler tries to run
  117. + * the reallocation while the bottom half is either running on another
  118. + * CPU or was interrupted on the same CPU. To get around this the
  119. + * driver uses bitops to prevent the reallocation routines from being
  120. + * reentered.
  121. + *
  122. + * TX handling can also be done without holding any spin lock, wheee
  123. + * this is fun! since tx_csm is only written to by the interrupt
  124. + * handler.
  125. + */
  126. +
  127. +/**
  128. + * Threshold values for RX buffer allocation - the low water marks for
  129. + * when to start refilling the rings are set to 75% of the ring
  130. + * sizes. It seems to make sense to refill the rings entirely from the
  131. + * intrrupt handler once it gets below the panic threshold, that way
  132. + * we don't risk that the refilling is moved to another CPU when the
  133. + * one running the interrupt handler just got the slab code hot in its
  134. + * cache.
  135. + */
  136. +#define RX_RING_SIZE AR2313_DESCR_ENTRIES
  137. +#define RX_PANIC_THRES (RX_RING_SIZE/4)
  138. +#define RX_LOW_THRES ((3*RX_RING_SIZE)/4)
  139. +#define CRC_LEN 4
  140. +#define RX_OFFSET 2
  141. +
  142. +#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  143. +#define VLAN_HDR 4
  144. +#else
  145. +#define VLAN_HDR 0
  146. +#endif
  147. +
  148. +#define AR2313_BUFSIZE (AR2313_MTU + VLAN_HDR + ETH_HLEN + CRC_LEN + \
  149. + RX_OFFSET)
  150. +
  151. +#ifdef MODULE
  152. +MODULE_LICENSE("GPL");
  153. +MODULE_AUTHOR("Sameer Dekate <[email protected]>, Imre Kaloz <[email protected]>, Felix Fietkau <[email protected]>");
  154. +MODULE_DESCRIPTION("AR231x Ethernet driver");
  155. +#endif
  156. +
  157. +/* prototypes */
  158. +static void ar231x_halt(struct net_device *dev);
  159. +static void rx_tasklet_func(unsigned long data);
  160. +static void rx_tasklet_cleanup(struct net_device *dev);
  161. +static void ar231x_multicast_list(struct net_device *dev);
  162. +static void ar231x_tx_timeout(struct net_device *dev, unsigned int txqueue);
  163. +
  164. +static int ar231x_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum);
  165. +static int ar231x_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  166. + u16 value);
  167. +static int ar231x_mdiobus_reset(struct mii_bus *bus);
  168. +static int ar231x_mdiobus_probe(struct net_device *dev);
  169. +static void ar231x_adjust_link(struct net_device *dev);
  170. +
  171. +#ifndef ERR
  172. +#define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
  173. +#endif
  174. +
  175. +#ifdef CONFIG_NET_POLL_CONTROLLER
  176. +static void
  177. +ar231x_netpoll(struct net_device *dev)
  178. +{
  179. + unsigned long flags;
  180. +
  181. + local_irq_save(flags);
  182. + ar231x_interrupt(dev->irq, dev);
  183. + local_irq_restore(flags);
  184. +}
  185. +#endif
  186. +
  187. +static const struct net_device_ops ar231x_ops = {
  188. + .ndo_open = ar231x_open,
  189. + .ndo_stop = ar231x_close,
  190. + .ndo_start_xmit = ar231x_start_xmit,
  191. + .ndo_set_rx_mode = ar231x_multicast_list,
  192. + .ndo_do_ioctl = ar231x_ioctl,
  193. + .ndo_validate_addr = eth_validate_addr,
  194. + .ndo_set_mac_address = eth_mac_addr,
  195. + .ndo_tx_timeout = ar231x_tx_timeout,
  196. +#ifdef CONFIG_NET_POLL_CONTROLLER
  197. + .ndo_poll_controller = ar231x_netpoll,
  198. +#endif
  199. +};
  200. +
  201. +static int ar231x_probe(struct platform_device *pdev)
  202. +{
  203. + struct net_device *dev;
  204. + struct ar231x_private *sp;
  205. + struct resource *res;
  206. + unsigned long ar_eth_base;
  207. + char buf[64];
  208. +
  209. + dev = alloc_etherdev(sizeof(struct ar231x_private));
  210. +
  211. + if (dev == NULL) {
  212. + printk(KERN_ERR
  213. + "ar231x: Unable to allocate net_device structure!\n");
  214. + return -ENOMEM;
  215. + }
  216. +
  217. + platform_set_drvdata(pdev, dev);
  218. +
  219. + SET_NETDEV_DEV(dev, &pdev->dev);
  220. +
  221. + sp = netdev_priv(dev);
  222. + sp->dev = dev;
  223. + sp->pdev = pdev;
  224. + sp->cfg = pdev->dev.platform_data;
  225. +
  226. + sprintf(buf, "eth%d_membase", pdev->id);
  227. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, buf);
  228. + if (!res)
  229. + return -ENODEV;
  230. +
  231. + sp->link = 0;
  232. + ar_eth_base = res->start;
  233. +
  234. + sprintf(buf, "eth%d_irq", pdev->id);
  235. + dev->irq = platform_get_irq_byname(pdev, buf);
  236. +
  237. + spin_lock_init(&sp->lock);
  238. +
  239. + dev->features |= NETIF_F_HIGHDMA;
  240. + dev->netdev_ops = &ar231x_ops;
  241. +
  242. + tasklet_init(&sp->rx_tasklet, rx_tasklet_func, (unsigned long)dev);
  243. + tasklet_disable(&sp->rx_tasklet);
  244. +
  245. + sp->eth_regs = ioremap(ar_eth_base, sizeof(*sp->eth_regs));
  246. + if (!sp->eth_regs) {
  247. + printk("Can't remap eth registers\n");
  248. + return -ENXIO;
  249. + }
  250. +
  251. + /**
  252. + * When there's only one MAC, PHY regs are typically on ENET0,
  253. + * even though the MAC might be on ENET1.
  254. + * So remap PHY regs separately.
  255. + */
  256. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eth0_mii");
  257. + if (!res) {
  258. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  259. + "eth1_mii");
  260. + if (!res)
  261. + return -ENODEV;
  262. + }
  263. + sp->phy_regs = ioremap(res->start, resource_size(res));
  264. + if (!sp->phy_regs) {
  265. + printk("Can't remap phy registers\n");
  266. + return -ENXIO;
  267. + }
  268. +
  269. + sp->dma_regs = ioremap(ar_eth_base + 0x1000,
  270. + sizeof(*sp->dma_regs));
  271. + if (!sp->dma_regs) {
  272. + printk("Can't remap DMA registers\n");
  273. + return -ENXIO;
  274. + }
  275. + dev->base_addr = ar_eth_base + 0x1000;
  276. +
  277. + strncpy(sp->name, "Atheros AR231x", sizeof(sp->name) - 1);
  278. + sp->name[sizeof(sp->name) - 1] = '\0';
  279. + memcpy(dev->dev_addr, sp->cfg->macaddr, 6);
  280. +
  281. + if (ar231x_init(dev)) {
  282. + /* ar231x_init() calls ar231x_init_cleanup() on error */
  283. + kfree(dev);
  284. + return -ENODEV;
  285. + }
  286. +
  287. + if (register_netdev(dev)) {
  288. + printk("%s: register_netdev failed\n", __func__);
  289. + return -1;
  290. + }
  291. +
  292. + printk("%s: %s: %pM, irq %d\n", dev->name, sp->name, dev->dev_addr,
  293. + dev->irq);
  294. +
  295. + sp->mii_bus = mdiobus_alloc();
  296. + if (sp->mii_bus == NULL)
  297. + return -1;
  298. +
  299. + sp->mii_bus->priv = dev;
  300. + sp->mii_bus->read = ar231x_mdiobus_read;
  301. + sp->mii_bus->write = ar231x_mdiobus_write;
  302. + sp->mii_bus->reset = ar231x_mdiobus_reset;
  303. + sp->mii_bus->name = "ar231x_eth_mii";
  304. + snprintf(sp->mii_bus->id, MII_BUS_ID_SIZE, "%d", pdev->id);
  305. +
  306. + mdiobus_register(sp->mii_bus);
  307. +
  308. + if (ar231x_mdiobus_probe(dev) != 0) {
  309. + printk(KERN_ERR "%s: mdiobus_probe failed\n", dev->name);
  310. + rx_tasklet_cleanup(dev);
  311. + ar231x_init_cleanup(dev);
  312. + unregister_netdev(dev);
  313. + kfree(dev);
  314. + return -ENODEV;
  315. + }
  316. +
  317. + return 0;
  318. +}
  319. +
  320. +static void ar231x_multicast_list(struct net_device *dev)
  321. +{
  322. + struct ar231x_private *sp = netdev_priv(dev);
  323. + unsigned int filter;
  324. +
  325. + filter = sp->eth_regs->mac_control;
  326. +
  327. + if (dev->flags & IFF_PROMISC)
  328. + filter |= MAC_CONTROL_PR;
  329. + else
  330. + filter &= ~MAC_CONTROL_PR;
  331. + if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 0))
  332. + filter |= MAC_CONTROL_PM;
  333. + else
  334. + filter &= ~MAC_CONTROL_PM;
  335. +
  336. + sp->eth_regs->mac_control = filter;
  337. +}
  338. +
  339. +static void rx_tasklet_cleanup(struct net_device *dev)
  340. +{
  341. + struct ar231x_private *sp = netdev_priv(dev);
  342. +
  343. + /**
  344. + * Tasklet may be scheduled. Need to get it removed from the list
  345. + * since we're about to free the struct.
  346. + */
  347. +
  348. + sp->unloading = 1;
  349. + tasklet_enable(&sp->rx_tasklet);
  350. + tasklet_kill(&sp->rx_tasklet);
  351. +}
  352. +
  353. +static int ar231x_remove(struct platform_device *pdev)
  354. +{
  355. + struct net_device *dev = platform_get_drvdata(pdev);
  356. + struct ar231x_private *sp = netdev_priv(dev);
  357. +
  358. + rx_tasklet_cleanup(dev);
  359. + ar231x_init_cleanup(dev);
  360. + unregister_netdev(dev);
  361. + mdiobus_unregister(sp->mii_bus);
  362. + mdiobus_free(sp->mii_bus);
  363. + kfree(dev);
  364. + return 0;
  365. +}
  366. +
  367. +/**
  368. + * Restart the AR2313 ethernet controller.
  369. + */
  370. +static int ar231x_restart(struct net_device *dev)
  371. +{
  372. + /* disable interrupts */
  373. + disable_irq(dev->irq);
  374. +
  375. + /* stop mac */
  376. + ar231x_halt(dev);
  377. +
  378. + /* initialize */
  379. + ar231x_init(dev);
  380. +
  381. + /* enable interrupts */
  382. + enable_irq(dev->irq);
  383. +
  384. + return 0;
  385. +}
  386. +
  387. +static struct platform_driver ar231x_driver = {
  388. + .driver.name = "ar231x-eth",
  389. + .probe = ar231x_probe,
  390. + .remove = ar231x_remove,
  391. +};
  392. +
  393. +module_platform_driver(ar231x_driver);
  394. +
  395. +static void ar231x_free_descriptors(struct net_device *dev)
  396. +{
  397. + struct ar231x_private *sp = netdev_priv(dev);
  398. +
  399. + if (sp->rx_ring != NULL) {
  400. + kfree((void *)KSEG0ADDR(sp->rx_ring));
  401. + sp->rx_ring = NULL;
  402. + sp->tx_ring = NULL;
  403. + }
  404. +}
  405. +
  406. +static int ar231x_allocate_descriptors(struct net_device *dev)
  407. +{
  408. + struct ar231x_private *sp = netdev_priv(dev);
  409. + int size;
  410. + int j;
  411. + ar231x_descr_t *space;
  412. +
  413. + if (sp->rx_ring != NULL) {
  414. + printk("%s: already done.\n", __func__);
  415. + return 0;
  416. + }
  417. +
  418. + size = sizeof(ar231x_descr_t) * (AR2313_DESCR_ENTRIES * AR2313_QUEUES);
  419. + space = kmalloc(size, GFP_KERNEL);
  420. + if (space == NULL)
  421. + return 1;
  422. +
  423. + /* invalidate caches */
  424. + dma_cache_inv((unsigned int)space, size);
  425. +
  426. + /* now convert pointer to KSEG1 */
  427. + space = (ar231x_descr_t *)KSEG1ADDR(space);
  428. +
  429. + memset((void *)space, 0, size);
  430. +
  431. + sp->rx_ring = space;
  432. + space += AR2313_DESCR_ENTRIES;
  433. +
  434. + sp->tx_ring = space;
  435. + space += AR2313_DESCR_ENTRIES;
  436. +
  437. + /* Initialize the transmit Descriptors */
  438. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  439. + ar231x_descr_t *td = &sp->tx_ring[j];
  440. +
  441. + td->status = 0;
  442. + td->devcs = DMA_TX1_CHAINED;
  443. + td->addr = 0;
  444. + td->descr = virt_to_phys(&sp->tx_ring[DSC_NEXT(j)]);
  445. + }
  446. +
  447. + return 0;
  448. +}
  449. +
  450. +/**
  451. + * Generic cleanup handling data allocated during init. Used when the
  452. + * module is unloaded or if an error occurs during initialization
  453. + */
  454. +static void ar231x_init_cleanup(struct net_device *dev)
  455. +{
  456. + struct ar231x_private *sp = netdev_priv(dev);
  457. + struct sk_buff *skb;
  458. + int j;
  459. +
  460. + ar231x_free_descriptors(dev);
  461. +
  462. + if (sp->eth_regs)
  463. + iounmap((void *)sp->eth_regs);
  464. + if (sp->dma_regs)
  465. + iounmap((void *)sp->dma_regs);
  466. + if (sp->phy_regs)
  467. + iounmap((void *)sp->phy_regs);
  468. +
  469. + if (sp->rx_skb) {
  470. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  471. + skb = sp->rx_skb[j];
  472. + if (skb) {
  473. + sp->rx_skb[j] = NULL;
  474. + dev_kfree_skb(skb);
  475. + }
  476. + }
  477. + kfree(sp->rx_skb);
  478. + sp->rx_skb = NULL;
  479. + }
  480. +
  481. + if (sp->tx_skb) {
  482. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  483. + skb = sp->tx_skb[j];
  484. + if (skb) {
  485. + sp->tx_skb[j] = NULL;
  486. + dev_kfree_skb(skb);
  487. + }
  488. + }
  489. + kfree(sp->tx_skb);
  490. + sp->tx_skb = NULL;
  491. + }
  492. +}
  493. +
  494. +static int ar231x_reset_reg(struct net_device *dev)
  495. +{
  496. + struct ar231x_private *sp = netdev_priv(dev);
  497. + unsigned int ethsal, ethsah;
  498. + unsigned int flags;
  499. +
  500. + sp->cfg->reset_set(sp->cfg->reset_mac);
  501. + mdelay(10);
  502. + sp->cfg->reset_clear(sp->cfg->reset_mac);
  503. + mdelay(10);
  504. + sp->cfg->reset_set(sp->cfg->reset_phy);
  505. + mdelay(10);
  506. + sp->cfg->reset_clear(sp->cfg->reset_phy);
  507. + mdelay(10);
  508. +
  509. + sp->dma_regs->bus_mode = (DMA_BUS_MODE_SWR);
  510. + mdelay(10);
  511. + sp->dma_regs->bus_mode =
  512. + ((32 << DMA_BUS_MODE_PBL_SHIFT) | DMA_BUS_MODE_BLE);
  513. +
  514. + /* enable interrupts */
  515. + sp->dma_regs->intr_ena = DMA_STATUS_AIS | DMA_STATUS_NIS |
  516. + DMA_STATUS_RI | DMA_STATUS_TI |
  517. + DMA_STATUS_FBE;
  518. + sp->dma_regs->xmt_base = virt_to_phys(sp->tx_ring);
  519. + sp->dma_regs->rcv_base = virt_to_phys(sp->rx_ring);
  520. + sp->dma_regs->control =
  521. + (DMA_CONTROL_SR | DMA_CONTROL_ST | DMA_CONTROL_SF);
  522. +
  523. + sp->eth_regs->flow_control = (FLOW_CONTROL_FCE);
  524. + sp->eth_regs->vlan_tag = (0x8100);
  525. +
  526. + /* Enable Ethernet Interface */
  527. + flags = (MAC_CONTROL_TE | /* transmit enable */
  528. + MAC_CONTROL_PM | /* pass mcast */
  529. + MAC_CONTROL_F | /* full duplex */
  530. + MAC_CONTROL_HBD); /* heart beat disabled */
  531. +
  532. + if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
  533. + flags |= MAC_CONTROL_PR;
  534. + }
  535. + sp->eth_regs->mac_control = flags;
  536. +
  537. + /* Set all Ethernet station address registers to their initial values */
  538. + ethsah = (((u_int) (dev->dev_addr[5]) << 8) & (u_int) 0x0000FF00) |
  539. + (((u_int) (dev->dev_addr[4]) << 0) & (u_int) 0x000000FF);
  540. +
  541. + ethsal = (((u_int) (dev->dev_addr[3]) << 24) & (u_int) 0xFF000000) |
  542. + (((u_int) (dev->dev_addr[2]) << 16) & (u_int) 0x00FF0000) |
  543. + (((u_int) (dev->dev_addr[1]) << 8) & (u_int) 0x0000FF00) |
  544. + (((u_int) (dev->dev_addr[0]) << 0) & (u_int) 0x000000FF);
  545. +
  546. + sp->eth_regs->mac_addr[0] = ethsah;
  547. + sp->eth_regs->mac_addr[1] = ethsal;
  548. +
  549. + mdelay(10);
  550. +
  551. + return 0;
  552. +}
  553. +
  554. +static int ar231x_init(struct net_device *dev)
  555. +{
  556. + struct ar231x_private *sp = netdev_priv(dev);
  557. + int ecode = 0;
  558. +
  559. + /* Allocate descriptors */
  560. + if (ar231x_allocate_descriptors(dev)) {
  561. + printk("%s: %s: ar231x_allocate_descriptors failed\n",
  562. + dev->name, __func__);
  563. + ecode = -EAGAIN;
  564. + goto init_error;
  565. + }
  566. +
  567. + /* Get the memory for the skb rings */
  568. + if (sp->rx_skb == NULL) {
  569. + sp->rx_skb =
  570. + kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
  571. + GFP_KERNEL);
  572. + if (!(sp->rx_skb)) {
  573. + printk("%s: %s: rx_skb kmalloc failed\n",
  574. + dev->name, __func__);
  575. + ecode = -EAGAIN;
  576. + goto init_error;
  577. + }
  578. + }
  579. + memset(sp->rx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
  580. +
  581. + if (sp->tx_skb == NULL) {
  582. + sp->tx_skb =
  583. + kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
  584. + GFP_KERNEL);
  585. + if (!(sp->tx_skb)) {
  586. + printk("%s: %s: tx_skb kmalloc failed\n",
  587. + dev->name, __func__);
  588. + ecode = -EAGAIN;
  589. + goto init_error;
  590. + }
  591. + }
  592. + memset(sp->tx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
  593. +
  594. + /**
  595. + * Set tx_csm before we start receiving interrupts, otherwise
  596. + * the interrupt handler might think it is supposed to process
  597. + * tx ints before we are up and running, which may cause a null
  598. + * pointer access in the int handler.
  599. + */
  600. + sp->rx_skbprd = 0;
  601. + sp->cur_rx = 0;
  602. + sp->tx_prd = 0;
  603. + sp->tx_csm = 0;
  604. +
  605. + /* Zero the stats before starting the interface */
  606. + memset(&dev->stats, 0, sizeof(dev->stats));
  607. +
  608. + /**
  609. + * We load the ring here as there seem to be no way to tell the
  610. + * firmware to wipe the ring without re-initializing it.
  611. + */
  612. + ar231x_load_rx_ring(dev, RX_RING_SIZE);
  613. +
  614. + /* Init hardware */
  615. + ar231x_reset_reg(dev);
  616. +
  617. + /* Get the IRQ */
  618. + ecode = request_irq(dev->irq, &ar231x_interrupt, 0,
  619. + dev->name, dev);
  620. + if (ecode) {
  621. + printk(KERN_WARNING "%s: %s: Requested IRQ %d is busy\n",
  622. + dev->name, __func__, dev->irq);
  623. + goto init_error;
  624. + }
  625. +
  626. + tasklet_enable(&sp->rx_tasklet);
  627. +
  628. + return 0;
  629. +
  630. +init_error:
  631. + ar231x_init_cleanup(dev);
  632. + return ecode;
  633. +}
  634. +
  635. +/**
  636. + * Load the rx ring.
  637. + *
  638. + * Loading rings is safe without holding the spin lock since this is
  639. + * done only before the device is enabled, thus no interrupts are
  640. + * generated and by the interrupt handler/tasklet handler.
  641. + */
  642. +static void ar231x_load_rx_ring(struct net_device *dev, int nr_bufs)
  643. +{
  644. + struct ar231x_private *sp = netdev_priv(dev);
  645. + short i, idx;
  646. +
  647. + idx = sp->rx_skbprd;
  648. +
  649. + for (i = 0; i < nr_bufs; i++) {
  650. + struct sk_buff *skb;
  651. + ar231x_descr_t *rd;
  652. +
  653. + if (sp->rx_skb[idx])
  654. + break;
  655. +
  656. + skb = netdev_alloc_skb_ip_align(dev, AR2313_BUFSIZE);
  657. + if (!skb) {
  658. + printk("\n\n\n\n %s: No memory in system\n\n\n\n",
  659. + __func__);
  660. + break;
  661. + }
  662. +
  663. + /* Make sure IP header starts on a fresh cache line */
  664. + skb->dev = dev;
  665. + sp->rx_skb[idx] = skb;
  666. +
  667. + rd = (ar231x_descr_t *)&sp->rx_ring[idx];
  668. +
  669. + /* initialize dma descriptor */
  670. + rd->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
  671. + DMA_RX1_CHAINED);
  672. + rd->addr = virt_to_phys(skb->data);
  673. + rd->descr = virt_to_phys(&sp->rx_ring[DSC_NEXT(idx)]);
  674. + rd->status = DMA_RX_OWN;
  675. +
  676. + idx = DSC_NEXT(idx);
  677. + }
  678. +
  679. + if (i)
  680. + sp->rx_skbprd = idx;
  681. +}
  682. +
  683. +#define AR2313_MAX_PKTS_PER_CALL 64
  684. +
  685. +static int ar231x_rx_int(struct net_device *dev)
  686. +{
  687. + struct ar231x_private *sp = netdev_priv(dev);
  688. + struct sk_buff *skb, *skb_new;
  689. + ar231x_descr_t *rxdesc;
  690. + unsigned int status;
  691. + u32 idx;
  692. + int pkts = 0;
  693. + int rval;
  694. +
  695. + idx = sp->cur_rx;
  696. +
  697. + /* process at most the entire ring and then wait for another int */
  698. + while (1) {
  699. + rxdesc = &sp->rx_ring[idx];
  700. + status = rxdesc->status;
  701. +
  702. + if (status & DMA_RX_OWN) {
  703. + /* SiByte owns descriptor or descr not yet filled in */
  704. + rval = 0;
  705. + break;
  706. + }
  707. +
  708. + if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
  709. + rval = 1;
  710. + break;
  711. + }
  712. +
  713. + if ((status & DMA_RX_ERROR) && !(status & DMA_RX_LONG)) {
  714. + dev->stats.rx_errors++;
  715. + dev->stats.rx_dropped++;
  716. +
  717. + /* add statistics counters */
  718. + if (status & DMA_RX_ERR_CRC)
  719. + dev->stats.rx_crc_errors++;
  720. + if (status & DMA_RX_ERR_COL)
  721. + dev->stats.rx_over_errors++;
  722. + if (status & DMA_RX_ERR_LENGTH)
  723. + dev->stats.rx_length_errors++;
  724. + if (status & DMA_RX_ERR_RUNT)
  725. + dev->stats.rx_over_errors++;
  726. + if (status & DMA_RX_ERR_DESC)
  727. + dev->stats.rx_over_errors++;
  728. +
  729. + } else {
  730. + /* alloc new buffer. */
  731. + skb_new = netdev_alloc_skb_ip_align(dev,
  732. + AR2313_BUFSIZE);
  733. + if (skb_new != NULL) {
  734. + skb = sp->rx_skb[idx];
  735. + /* set skb */
  736. + skb_put(skb, ((status >> DMA_RX_LEN_SHIFT) &
  737. + 0x3fff) - CRC_LEN);
  738. +
  739. + dev->stats.rx_bytes += skb->len;
  740. + skb->protocol = eth_type_trans(skb, dev);
  741. + /* pass the packet to upper layers */
  742. + netif_rx(skb);
  743. +
  744. + skb_new->dev = dev;
  745. + /* reset descriptor's curr_addr */
  746. + rxdesc->addr = virt_to_phys(skb_new->data);
  747. +
  748. + dev->stats.rx_packets++;
  749. + sp->rx_skb[idx] = skb_new;
  750. + } else {
  751. + dev->stats.rx_dropped++;
  752. + }
  753. + }
  754. +
  755. + rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
  756. + DMA_RX1_CHAINED);
  757. + rxdesc->status = DMA_RX_OWN;
  758. +
  759. + idx = DSC_NEXT(idx);
  760. + }
  761. +
  762. + sp->cur_rx = idx;
  763. +
  764. + return rval;
  765. +}
  766. +
  767. +static void ar231x_tx_int(struct net_device *dev)
  768. +{
  769. + struct ar231x_private *sp = netdev_priv(dev);
  770. + u32 idx;
  771. + struct sk_buff *skb;
  772. + ar231x_descr_t *txdesc;
  773. + unsigned int status = 0;
  774. +
  775. + idx = sp->tx_csm;
  776. +
  777. + while (idx != sp->tx_prd) {
  778. + txdesc = &sp->tx_ring[idx];
  779. + status = txdesc->status;
  780. +
  781. + if (status & DMA_TX_OWN) {
  782. + /* ar231x dma still owns descr */
  783. + break;
  784. + }
  785. + /* done with this descriptor */
  786. + dma_unmap_single(&sp->pdev->dev, txdesc->addr,
  787. + txdesc->devcs & DMA_TX1_BSIZE_MASK,
  788. + DMA_TO_DEVICE);
  789. + txdesc->status = 0;
  790. +
  791. + if (status & DMA_TX_ERROR) {
  792. + dev->stats.tx_errors++;
  793. + dev->stats.tx_dropped++;
  794. + if (status & DMA_TX_ERR_UNDER)
  795. + dev->stats.tx_fifo_errors++;
  796. + if (status & DMA_TX_ERR_HB)
  797. + dev->stats.tx_heartbeat_errors++;
  798. + if (status & (DMA_TX_ERR_LOSS | DMA_TX_ERR_LINK))
  799. + dev->stats.tx_carrier_errors++;
  800. + if (status & (DMA_TX_ERR_LATE | DMA_TX_ERR_COL |
  801. + DMA_TX_ERR_JABBER | DMA_TX_ERR_DEFER))
  802. + dev->stats.tx_aborted_errors++;
  803. + } else {
  804. + /* transmit OK */
  805. + dev->stats.tx_packets++;
  806. + }
  807. +
  808. + skb = sp->tx_skb[idx];
  809. + sp->tx_skb[idx] = NULL;
  810. + idx = DSC_NEXT(idx);
  811. + dev->stats.tx_bytes += skb->len;
  812. + dev_kfree_skb_irq(skb);
  813. + }
  814. +
  815. + sp->tx_csm = idx;
  816. +}
  817. +
  818. +static void rx_tasklet_func(unsigned long data)
  819. +{
  820. + struct net_device *dev = (struct net_device *)data;
  821. + struct ar231x_private *sp = netdev_priv(dev);
  822. +
  823. + if (sp->unloading)
  824. + return;
  825. +
  826. + if (ar231x_rx_int(dev)) {
  827. + tasklet_hi_schedule(&sp->rx_tasklet);
  828. + } else {
  829. + unsigned long flags;
  830. +
  831. + spin_lock_irqsave(&sp->lock, flags);
  832. + sp->dma_regs->intr_ena |= DMA_STATUS_RI;
  833. + spin_unlock_irqrestore(&sp->lock, flags);
  834. + }
  835. +}
  836. +
  837. +static void rx_schedule(struct net_device *dev)
  838. +{
  839. + struct ar231x_private *sp = netdev_priv(dev);
  840. +
  841. + sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
  842. +
  843. + tasklet_hi_schedule(&sp->rx_tasklet);
  844. +}
  845. +
  846. +static irqreturn_t ar231x_interrupt(int irq, void *dev_id)
  847. +{
  848. + struct net_device *dev = (struct net_device *)dev_id;
  849. + struct ar231x_private *sp = netdev_priv(dev);
  850. + unsigned int status, enabled;
  851. +
  852. + /* clear interrupt */
  853. + /* Don't clear RI bit if currently disabled */
  854. + status = sp->dma_regs->status;
  855. + enabled = sp->dma_regs->intr_ena;
  856. + sp->dma_regs->status = status & enabled;
  857. +
  858. + if (status & DMA_STATUS_NIS) {
  859. + /* normal status */
  860. + /**
  861. + * Don't schedule rx processing if interrupt
  862. + * is already disabled.
  863. + */
  864. + if (status & enabled & DMA_STATUS_RI) {
  865. + /* receive interrupt */
  866. + rx_schedule(dev);
  867. + }
  868. + if (status & DMA_STATUS_TI) {
  869. + /* transmit interrupt */
  870. + ar231x_tx_int(dev);
  871. + }
  872. + }
  873. +
  874. + /* abnormal status */
  875. + if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS))
  876. + ar231x_restart(dev);
  877. +
  878. + return IRQ_HANDLED;
  879. +}
  880. +
  881. +static int ar231x_open(struct net_device *dev)
  882. +{
  883. + struct ar231x_private *sp = netdev_priv(dev);
  884. + unsigned int ethsal, ethsah;
  885. +
  886. + /* reset the hardware, in case the MAC address changed */
  887. + ethsah = (((u_int) (dev->dev_addr[5]) << 8) & (u_int) 0x0000FF00) |
  888. + (((u_int) (dev->dev_addr[4]) << 0) & (u_int) 0x000000FF);
  889. +
  890. + ethsal = (((u_int) (dev->dev_addr[3]) << 24) & (u_int) 0xFF000000) |
  891. + (((u_int) (dev->dev_addr[2]) << 16) & (u_int) 0x00FF0000) |
  892. + (((u_int) (dev->dev_addr[1]) << 8) & (u_int) 0x0000FF00) |
  893. + (((u_int) (dev->dev_addr[0]) << 0) & (u_int) 0x000000FF);
  894. +
  895. + sp->eth_regs->mac_addr[0] = ethsah;
  896. + sp->eth_regs->mac_addr[1] = ethsal;
  897. +
  898. + mdelay(10);
  899. +
  900. + dev->mtu = 1500;
  901. + netif_start_queue(dev);
  902. +
  903. + sp->eth_regs->mac_control |= MAC_CONTROL_RE;
  904. +
  905. + phy_start(sp->phy_dev);
  906. +
  907. + return 0;
  908. +}
  909. +
  910. +static void ar231x_tx_timeout(struct net_device *dev, unsigned int txqueue)
  911. +{
  912. + struct ar231x_private *sp = netdev_priv(dev);
  913. + unsigned long flags;
  914. +
  915. + spin_lock_irqsave(&sp->lock, flags);
  916. + ar231x_restart(dev);
  917. + spin_unlock_irqrestore(&sp->lock, flags);
  918. +}
  919. +
  920. +static void ar231x_halt(struct net_device *dev)
  921. +{
  922. + struct ar231x_private *sp = netdev_priv(dev);
  923. + int j;
  924. +
  925. + tasklet_disable(&sp->rx_tasklet);
  926. +
  927. + /* kill the MAC */
  928. + sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
  929. + MAC_CONTROL_TE); /* disable Transmits */
  930. + /* stop dma */
  931. + sp->dma_regs->control = 0;
  932. + sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
  933. +
  934. + /* place phy and MAC in reset */
  935. + sp->cfg->reset_set(sp->cfg->reset_mac);
  936. + sp->cfg->reset_set(sp->cfg->reset_phy);
  937. +
  938. + /* free buffers on tx ring */
  939. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  940. + struct sk_buff *skb;
  941. + ar231x_descr_t *txdesc;
  942. +
  943. + txdesc = &sp->tx_ring[j];
  944. + txdesc->descr = 0;
  945. +
  946. + skb = sp->tx_skb[j];
  947. + if (skb) {
  948. + dev_kfree_skb(skb);
  949. + sp->tx_skb[j] = NULL;
  950. + }
  951. + }
  952. +}
  953. +
  954. +/**
  955. + * close should do nothing. Here's why. It's called when
  956. + * 'ifconfig bond0 down' is run. If it calls free_irq then
  957. + * the irq is gone forever ! When bond0 is made 'up' again,
  958. + * the ar231x_open () does not call request_irq (). Worse,
  959. + * the call to ar231x_halt() generates a WDOG reset due to
  960. + * the write to reset register and the box reboots.
  961. + * Commenting this out is good since it allows the
  962. + * system to resume when bond0 is made up again.
  963. + */
  964. +static int ar231x_close(struct net_device *dev)
  965. +{
  966. + struct ar231x_private *sp = netdev_priv(dev);
  967. +#if 0
  968. + /* Disable interrupts */
  969. + disable_irq(dev->irq);
  970. +
  971. + /**
  972. + * Without (or before) releasing irq and stopping hardware, this
  973. + * is an absolute non-sense, by the way. It will be reset instantly
  974. + * by the first irq.
  975. + */
  976. + netif_stop_queue(dev);
  977. +
  978. + /* stop the MAC and DMA engines */
  979. + ar231x_halt(dev);
  980. +
  981. + /* release the interrupt */
  982. + free_irq(dev->irq, dev);
  983. +
  984. +#endif
  985. +
  986. + phy_stop(sp->phy_dev);
  987. +
  988. + return 0;
  989. +}
  990. +
  991. +static int ar231x_start_xmit(struct sk_buff *skb, struct net_device *dev)
  992. +{
  993. + struct ar231x_private *sp = netdev_priv(dev);
  994. + ar231x_descr_t *td;
  995. + u32 idx;
  996. +
  997. + idx = sp->tx_prd;
  998. + td = &sp->tx_ring[idx];
  999. +
  1000. + if (td->status & DMA_TX_OWN) {
  1001. + /* free skbuf and lie to the caller that we sent it out */
  1002. + dev->stats.tx_dropped++;
  1003. + dev_kfree_skb(skb);
  1004. +
  1005. + /* restart transmitter in case locked */
  1006. + sp->dma_regs->xmt_poll = 0;
  1007. + return 0;
  1008. + }
  1009. +
  1010. + /* Setup the transmit descriptor. */
  1011. + td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
  1012. + (DMA_TX1_LS | DMA_TX1_IC | DMA_TX1_CHAINED));
  1013. + td->addr = dma_map_single(&sp->pdev->dev, skb->data, skb->len, DMA_TO_DEVICE);
  1014. + td->status = DMA_TX_OWN;
  1015. +
  1016. + /* kick transmitter last */
  1017. + sp->dma_regs->xmt_poll = 0;
  1018. +
  1019. + sp->tx_skb[idx] = skb;
  1020. + idx = DSC_NEXT(idx);
  1021. + sp->tx_prd = idx;
  1022. +
  1023. + return 0;
  1024. +}
  1025. +
  1026. +static int ar231x_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1027. +{
  1028. + struct ar231x_private *sp = netdev_priv(dev);
  1029. +
  1030. + switch (cmd) {
  1031. + case SIOCGMIIPHY:
  1032. + case SIOCGMIIREG:
  1033. + case SIOCSMIIREG:
  1034. + return phy_mii_ioctl(sp->phy_dev, ifr, cmd);
  1035. +
  1036. + default:
  1037. + break;
  1038. + }
  1039. +
  1040. + return -EOPNOTSUPP;
  1041. +}
  1042. +
  1043. +static void ar231x_adjust_link(struct net_device *dev)
  1044. +{
  1045. + struct ar231x_private *sp = netdev_priv(dev);
  1046. + struct phy_device *phydev = sp->phy_dev;
  1047. + u32 mc;
  1048. +
  1049. + if (!phydev->link) {
  1050. + if (sp->link) {
  1051. + pr_info("%s: link down\n", dev->name);
  1052. + sp->link = 0;
  1053. + }
  1054. + return;
  1055. + }
  1056. + sp->link = 1;
  1057. +
  1058. + pr_info("%s: link up (%uMbps/%s duplex)\n", dev->name,
  1059. + phydev->speed, phydev->duplex ? "full" : "half");
  1060. +
  1061. + mc = sp->eth_regs->mac_control;
  1062. + if (phydev->duplex)
  1063. + mc = (mc | MAC_CONTROL_F) & ~MAC_CONTROL_DRO;
  1064. + else
  1065. + mc = (mc | MAC_CONTROL_DRO) & ~MAC_CONTROL_F;
  1066. + sp->eth_regs->mac_control = mc;
  1067. + sp->duplex = phydev->duplex;
  1068. +}
  1069. +
  1070. +#define MII_ADDR(phy, reg) \
  1071. + ((reg << MII_ADDR_REG_SHIFT) | (phy << MII_ADDR_PHY_SHIFT))
  1072. +
  1073. +static int
  1074. +ar231x_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  1075. +{
  1076. + struct net_device *const dev = bus->priv;
  1077. + struct ar231x_private *sp = netdev_priv(dev);
  1078. + volatile MII *ethernet = sp->phy_regs;
  1079. +
  1080. + ethernet->mii_addr = MII_ADDR(phy_addr, regnum);
  1081. + while (ethernet->mii_addr & MII_ADDR_BUSY)
  1082. + ;
  1083. + return ethernet->mii_data >> MII_DATA_SHIFT;
  1084. +}
  1085. +
  1086. +static int
  1087. +ar231x_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, u16 value)
  1088. +{
  1089. + struct net_device *const dev = bus->priv;
  1090. + struct ar231x_private *sp = netdev_priv(dev);
  1091. + volatile MII *ethernet = sp->phy_regs;
  1092. +
  1093. + while (ethernet->mii_addr & MII_ADDR_BUSY)
  1094. + ;
  1095. + ethernet->mii_data = value << MII_DATA_SHIFT;
  1096. + ethernet->mii_addr = MII_ADDR(phy_addr, regnum) | MII_ADDR_WRITE;
  1097. +
  1098. + return 0;
  1099. +}
  1100. +
  1101. +static int ar231x_mdiobus_reset(struct mii_bus *bus)
  1102. +{
  1103. + struct net_device *const dev = bus->priv;
  1104. +
  1105. + ar231x_reset_reg(dev);
  1106. +
  1107. + return 0;
  1108. +}
  1109. +
  1110. +static int ar231x_mdiobus_probe(struct net_device *dev)
  1111. +{
  1112. + __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  1113. + struct ar231x_private *const sp = netdev_priv(dev);
  1114. + struct phy_device *phydev = NULL;
  1115. +
  1116. + /* find the first (lowest address) PHY on the current MAC's MII bus */
  1117. + phydev = phy_find_first(sp->mii_bus);
  1118. + if (!phydev) {
  1119. + printk(KERN_ERR "ar231x: %s: no PHY found\n", dev->name);
  1120. + return -1;
  1121. + }
  1122. +
  1123. + /* now we are supposed to have a proper phydev, to attach to... */
  1124. + BUG_ON(phydev->attached_dev);
  1125. +
  1126. + phydev = phy_connect(dev, phydev_name(phydev), &ar231x_adjust_link,
  1127. + PHY_INTERFACE_MODE_MII);
  1128. + if (IS_ERR(phydev)) {
  1129. + printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  1130. + return PTR_ERR(phydev);
  1131. + }
  1132. +
  1133. + /* mask with MAC supported features */
  1134. + linkmode_set_bit_array(phy_10_100_features_array,
  1135. + ARRAY_SIZE(phy_10_100_features_array),
  1136. + mask);
  1137. + linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask);
  1138. + linkmode_set_bit(ETHTOOL_LINK_MODE_MII_BIT, mask);
  1139. + linkmode_set_bit(ETHTOOL_LINK_MODE_TP_BIT, mask);
  1140. +
  1141. + linkmode_and(phydev->supported, phydev->supported, mask);
  1142. + linkmode_copy(phydev->advertising, phydev->supported);
  1143. +
  1144. + sp->phy_dev = phydev;
  1145. +
  1146. + printk(KERN_INFO "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  1147. + dev->name, phydev->drv->name, phydev_name(phydev));
  1148. +
  1149. + return 0;
  1150. +}
  1151. +
  1152. --- /dev/null
  1153. +++ b/drivers/net/ethernet/atheros/ar231x/ar231x.h
  1154. @@ -0,0 +1,282 @@
  1155. +/*
  1156. + * ar231x.h: Linux driver for the Atheros AR231x Ethernet device.
  1157. + *
  1158. + * Copyright (C) 2004 by Sameer Dekate <[email protected]>
  1159. + * Copyright (C) 2006 Imre Kaloz <[email protected]>
  1160. + * Copyright (C) 2006-2009 Felix Fietkau <[email protected]>
  1161. + *
  1162. + * Thanks to Atheros for providing hardware and documentation
  1163. + * enabling me to write this driver.
  1164. + *
  1165. + * This program is free software; you can redistribute it and/or modify
  1166. + * it under the terms of the GNU General Public License as published by
  1167. + * the Free Software Foundation; either version 2 of the License, or
  1168. + * (at your option) any later version.
  1169. + */
  1170. +
  1171. +#ifndef _AR2313_H_
  1172. +#define _AR2313_H_
  1173. +
  1174. +#include <linux/interrupt.h>
  1175. +#include <generated/autoconf.h>
  1176. +#include <linux/bitops.h>
  1177. +#include <ath25_platform.h>
  1178. +
  1179. +/* probe link timer - 5 secs */
  1180. +#define LINK_TIMER (5*HZ)
  1181. +
  1182. +#define IS_DMA_TX_INT(X) (((X) & (DMA_STATUS_TI)) != 0)
  1183. +#define IS_DMA_RX_INT(X) (((X) & (DMA_STATUS_RI)) != 0)
  1184. +#define IS_DRIVER_OWNED(X) (((X) & (DMA_TX_OWN)) == 0)
  1185. +
  1186. +#define AR2313_TX_TIMEOUT (HZ/4)
  1187. +
  1188. +/* Rings */
  1189. +#define DSC_RING_ENTRIES_SIZE (AR2313_DESCR_ENTRIES * sizeof(struct desc))
  1190. +#define DSC_NEXT(idx) ((idx + 1) & (AR2313_DESCR_ENTRIES - 1))
  1191. +
  1192. +#define AR2313_MBGET 2
  1193. +#define AR2313_MBSET 3
  1194. +#define AR2313_PCI_RECONFIG 4
  1195. +#define AR2313_PCI_DUMP 5
  1196. +#define AR2313_TEST_PANIC 6
  1197. +#define AR2313_TEST_NULLPTR 7
  1198. +#define AR2313_READ_DATA 8
  1199. +#define AR2313_WRITE_DATA 9
  1200. +#define AR2313_GET_VERSION 10
  1201. +#define AR2313_TEST_HANG 11
  1202. +#define AR2313_SYNC 12
  1203. +
  1204. +#define DMA_RX_ERR_CRC BIT(1)
  1205. +#define DMA_RX_ERR_DRIB BIT(2)
  1206. +#define DMA_RX_ERR_MII BIT(3)
  1207. +#define DMA_RX_EV2 BIT(5)
  1208. +#define DMA_RX_ERR_COL BIT(6)
  1209. +#define DMA_RX_LONG BIT(7)
  1210. +#define DMA_RX_LS BIT(8) /* last descriptor */
  1211. +#define DMA_RX_FS BIT(9) /* first descriptor */
  1212. +#define DMA_RX_MF BIT(10) /* multicast frame */
  1213. +#define DMA_RX_ERR_RUNT BIT(11) /* runt frame */
  1214. +#define DMA_RX_ERR_LENGTH BIT(12) /* length error */
  1215. +#define DMA_RX_ERR_DESC BIT(14) /* descriptor error */
  1216. +#define DMA_RX_ERROR BIT(15) /* error summary */
  1217. +#define DMA_RX_LEN_MASK 0x3fff0000
  1218. +#define DMA_RX_LEN_SHIFT 16
  1219. +#define DMA_RX_FILT BIT(30)
  1220. +#define DMA_RX_OWN BIT(31) /* desc owned by DMA controller */
  1221. +
  1222. +#define DMA_RX1_BSIZE_MASK 0x000007ff
  1223. +#define DMA_RX1_BSIZE_SHIFT 0
  1224. +#define DMA_RX1_CHAINED BIT(24)
  1225. +#define DMA_RX1_RER BIT(25)
  1226. +
  1227. +#define DMA_TX_ERR_UNDER BIT(1) /* underflow error */
  1228. +#define DMA_TX_ERR_DEFER BIT(2) /* excessive deferral */
  1229. +#define DMA_TX_COL_MASK 0x78
  1230. +#define DMA_TX_COL_SHIFT 3
  1231. +#define DMA_TX_ERR_HB BIT(7) /* hearbeat failure */
  1232. +#define DMA_TX_ERR_COL BIT(8) /* excessive collisions */
  1233. +#define DMA_TX_ERR_LATE BIT(9) /* late collision */
  1234. +#define DMA_TX_ERR_LINK BIT(10) /* no carrier */
  1235. +#define DMA_TX_ERR_LOSS BIT(11) /* loss of carrier */
  1236. +#define DMA_TX_ERR_JABBER BIT(14) /* transmit jabber timeout */
  1237. +#define DMA_TX_ERROR BIT(15) /* frame aborted */
  1238. +#define DMA_TX_OWN BIT(31) /* descr owned by DMA controller */
  1239. +
  1240. +#define DMA_TX1_BSIZE_MASK 0x000007ff
  1241. +#define DMA_TX1_BSIZE_SHIFT 0
  1242. +#define DMA_TX1_CHAINED BIT(24) /* chained descriptors */
  1243. +#define DMA_TX1_TER BIT(25) /* transmit end of ring */
  1244. +#define DMA_TX1_FS BIT(29) /* first segment */
  1245. +#define DMA_TX1_LS BIT(30) /* last segment */
  1246. +#define DMA_TX1_IC BIT(31) /* interrupt on completion */
  1247. +
  1248. +#define RCVPKT_LENGTH(X) (X >> 16) /* Received pkt Length */
  1249. +
  1250. +#define MAC_CONTROL_RE BIT(2) /* receive enable */
  1251. +#define MAC_CONTROL_TE BIT(3) /* transmit enable */
  1252. +#define MAC_CONTROL_DC BIT(5) /* Deferral check */
  1253. +#define MAC_CONTROL_ASTP BIT(8) /* Auto pad strip */
  1254. +#define MAC_CONTROL_DRTY BIT(10) /* Disable retry */
  1255. +#define MAC_CONTROL_DBF BIT(11) /* Disable bcast frames */
  1256. +#define MAC_CONTROL_LCC BIT(12) /* late collision ctrl */
  1257. +#define MAC_CONTROL_HP BIT(13) /* Hash Perfect filtering */
  1258. +#define MAC_CONTROL_HASH BIT(14) /* Unicast hash filtering */
  1259. +#define MAC_CONTROL_HO BIT(15) /* Hash only filtering */
  1260. +#define MAC_CONTROL_PB BIT(16) /* Pass Bad frames */
  1261. +#define MAC_CONTROL_IF BIT(17) /* Inverse filtering */
  1262. +#define MAC_CONTROL_PR BIT(18) /* promis mode (valid frames only) */
  1263. +#define MAC_CONTROL_PM BIT(19) /* pass multicast */
  1264. +#define MAC_CONTROL_F BIT(20) /* full-duplex */
  1265. +#define MAC_CONTROL_DRO BIT(23) /* Disable Receive Own */
  1266. +#define MAC_CONTROL_HBD BIT(28) /* heart-beat disabled (MUST BE SET) */
  1267. +#define MAC_CONTROL_BLE BIT(30) /* big endian mode */
  1268. +#define MAC_CONTROL_RA BIT(31) /* rcv all (valid and invalid frames) */
  1269. +
  1270. +#define MII_ADDR_BUSY BIT(0)
  1271. +#define MII_ADDR_WRITE BIT(1)
  1272. +#define MII_ADDR_REG_SHIFT 6
  1273. +#define MII_ADDR_PHY_SHIFT 11
  1274. +#define MII_DATA_SHIFT 0
  1275. +
  1276. +#define FLOW_CONTROL_FCE BIT(1)
  1277. +
  1278. +#define DMA_BUS_MODE_SWR BIT(0) /* software reset */
  1279. +#define DMA_BUS_MODE_BLE BIT(7) /* big endian mode */
  1280. +#define DMA_BUS_MODE_PBL_SHIFT 8 /* programmable burst length 32 */
  1281. +#define DMA_BUS_MODE_DBO BIT(20) /* big-endian descriptors */
  1282. +
  1283. +#define DMA_STATUS_TI BIT(0) /* transmit interrupt */
  1284. +#define DMA_STATUS_TPS BIT(1) /* transmit process stopped */
  1285. +#define DMA_STATUS_TU BIT(2) /* transmit buffer unavailable */
  1286. +#define DMA_STATUS_TJT BIT(3) /* transmit buffer timeout */
  1287. +#define DMA_STATUS_UNF BIT(5) /* transmit underflow */
  1288. +#define DMA_STATUS_RI BIT(6) /* receive interrupt */
  1289. +#define DMA_STATUS_RU BIT(7) /* receive buffer unavailable */
  1290. +#define DMA_STATUS_RPS BIT(8) /* receive process stopped */
  1291. +#define DMA_STATUS_ETI BIT(10) /* early transmit interrupt */
  1292. +#define DMA_STATUS_FBE BIT(13) /* fatal bus interrupt */
  1293. +#define DMA_STATUS_ERI BIT(14) /* early receive interrupt */
  1294. +#define DMA_STATUS_AIS BIT(15) /* abnormal interrupt summary */
  1295. +#define DMA_STATUS_NIS BIT(16) /* normal interrupt summary */
  1296. +#define DMA_STATUS_RS_SHIFT 17 /* receive process state */
  1297. +#define DMA_STATUS_TS_SHIFT 20 /* transmit process state */
  1298. +#define DMA_STATUS_EB_SHIFT 23 /* error bits */
  1299. +
  1300. +#define DMA_CONTROL_SR BIT(1) /* start receive */
  1301. +#define DMA_CONTROL_ST BIT(13) /* start transmit */
  1302. +#define DMA_CONTROL_SF BIT(21) /* store and forward */
  1303. +
  1304. +typedef struct {
  1305. + volatile unsigned int status; /* OWN, Device control and status. */
  1306. + volatile unsigned int devcs; /* pkt Control bits + Length */
  1307. + volatile unsigned int addr; /* Current Address. */
  1308. + volatile unsigned int descr; /* Next descriptor in chain. */
  1309. +} ar231x_descr_t;
  1310. +
  1311. +/**
  1312. + * New Combo structure for Both Eth0 AND eth1
  1313. + *
  1314. + * Don't directly access MII related regs since phy chip could be actually
  1315. + * connected to another ethernet block.
  1316. + */
  1317. +typedef struct {
  1318. + volatile unsigned int mac_control; /* 0x00 */
  1319. + volatile unsigned int mac_addr[2]; /* 0x04 - 0x08 */
  1320. + volatile unsigned int mcast_table[2]; /* 0x0c - 0x10 */
  1321. + volatile unsigned int __mii_addr; /* 0x14 */
  1322. + volatile unsigned int __mii_data; /* 0x18 */
  1323. + volatile unsigned int flow_control; /* 0x1c */
  1324. + volatile unsigned int vlan_tag; /* 0x20 */
  1325. + volatile unsigned int pad[7]; /* 0x24 - 0x3c */
  1326. + volatile unsigned int ucast_table[8]; /* 0x40-0x5c */
  1327. +} ETHERNET_STRUCT;
  1328. +
  1329. +typedef struct {
  1330. + volatile unsigned int mii_addr;
  1331. + volatile unsigned int mii_data;
  1332. +} MII;
  1333. +
  1334. +/********************************************************************
  1335. + * Interrupt controller
  1336. + ********************************************************************/
  1337. +
  1338. +typedef struct {
  1339. + volatile unsigned int wdog_control; /* 0x08 */
  1340. + volatile unsigned int wdog_timer; /* 0x0c */
  1341. + volatile unsigned int misc_status; /* 0x10 */
  1342. + volatile unsigned int misc_mask; /* 0x14 */
  1343. + volatile unsigned int global_status; /* 0x18 */
  1344. + volatile unsigned int reserved; /* 0x1c */
  1345. + volatile unsigned int reset_control; /* 0x20 */
  1346. +} INTERRUPT;
  1347. +
  1348. +/********************************************************************
  1349. + * DMA controller
  1350. + ********************************************************************/
  1351. +typedef struct {
  1352. + volatile unsigned int bus_mode; /* 0x00 (CSR0) */
  1353. + volatile unsigned int xmt_poll; /* 0x04 (CSR1) */
  1354. + volatile unsigned int rcv_poll; /* 0x08 (CSR2) */
  1355. + volatile unsigned int rcv_base; /* 0x0c (CSR3) */
  1356. + volatile unsigned int xmt_base; /* 0x10 (CSR4) */
  1357. + volatile unsigned int status; /* 0x14 (CSR5) */
  1358. + volatile unsigned int control; /* 0x18 (CSR6) */
  1359. + volatile unsigned int intr_ena; /* 0x1c (CSR7) */
  1360. + volatile unsigned int rcv_missed; /* 0x20 (CSR8) */
  1361. + volatile unsigned int reserved[11]; /* 0x24-0x4c (CSR9-19) */
  1362. + volatile unsigned int cur_tx_buf_addr; /* 0x50 (CSR20) */
  1363. + volatile unsigned int cur_rx_buf_addr; /* 0x50 (CSR21) */
  1364. +} DMA;
  1365. +
  1366. +/**
  1367. + * Struct private for the Sibyte.
  1368. + *
  1369. + * Elements are grouped so variables used by the tx handling goes
  1370. + * together, and will go into the same cache lines etc. in order to
  1371. + * avoid cache line contention between the rx and tx handling on SMP.
  1372. + *
  1373. + * Frequently accessed variables are put at the beginning of the
  1374. + * struct to help the compiler generate better/shorter code.
  1375. + */
  1376. +struct ar231x_private {
  1377. + struct net_device *dev;
  1378. + struct platform_device *pdev;
  1379. + int version;
  1380. + u32 mb[2];
  1381. +
  1382. + volatile MII *phy_regs;
  1383. + volatile ETHERNET_STRUCT *eth_regs;
  1384. + volatile DMA *dma_regs;
  1385. + struct ar231x_eth *cfg;
  1386. +
  1387. + spinlock_t lock; /* Serialise access to device */
  1388. +
  1389. + /* RX and TX descriptors, must be adjacent */
  1390. + ar231x_descr_t *rx_ring;
  1391. + ar231x_descr_t *tx_ring;
  1392. +
  1393. + struct sk_buff **rx_skb;
  1394. + struct sk_buff **tx_skb;
  1395. +
  1396. + /* RX elements */
  1397. + u32 rx_skbprd;
  1398. + u32 cur_rx;
  1399. +
  1400. + /* TX elements */
  1401. + u32 tx_prd;
  1402. + u32 tx_csm;
  1403. +
  1404. + /* Misc elements */
  1405. + char name[48];
  1406. + struct {
  1407. + u32 address;
  1408. + u32 length;
  1409. + char *mapping;
  1410. + } desc;
  1411. +
  1412. + unsigned short link; /* 0 - link down, 1 - link up */
  1413. + unsigned short duplex; /* 0 - half, 1 - full */
  1414. +
  1415. + struct tasklet_struct rx_tasklet;
  1416. + int unloading;
  1417. +
  1418. + struct phy_device *phy_dev;
  1419. + struct mii_bus *mii_bus;
  1420. +};
  1421. +
  1422. +/* Prototypes */
  1423. +static int ar231x_init(struct net_device *dev);
  1424. +#ifdef TX_TIMEOUT
  1425. +static void ar231x_tx_timeout(struct net_device *dev);
  1426. +#endif
  1427. +static int ar231x_restart(struct net_device *dev);
  1428. +static void ar231x_load_rx_ring(struct net_device *dev, int bufs);
  1429. +static irqreturn_t ar231x_interrupt(int irq, void *dev_id);
  1430. +static int ar231x_open(struct net_device *dev);
  1431. +static int ar231x_start_xmit(struct sk_buff *skb, struct net_device *dev);
  1432. +static int ar231x_close(struct net_device *dev);
  1433. +static int ar231x_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  1434. +static void ar231x_init_cleanup(struct net_device *dev);
  1435. +
  1436. +#endif /* _AR2313_H_ */
  1437. --- a/arch/mips/ath25/ar2315_regs.h
  1438. +++ b/arch/mips/ath25/ar2315_regs.h
  1439. @@ -57,6 +57,9 @@
  1440. #define AR2315_PCI_EXT_BASE 0x80000000 /* PCI external */
  1441. #define AR2315_PCI_EXT_SIZE 0x40000000
  1442. +/* MII registers offset inside Ethernet MMR region */
  1443. +#define AR2315_ENET0_MII_BASE (AR2315_ENET0_BASE + 0x14)
  1444. +
  1445. /*
  1446. * Configuration registers
  1447. */
  1448. --- a/arch/mips/ath25/ar5312_regs.h
  1449. +++ b/arch/mips/ath25/ar5312_regs.h
  1450. @@ -64,6 +64,10 @@
  1451. #define AR5312_AR5312_REV7 0x0057 /* AR5312 WMAC (AP30-040) */
  1452. #define AR5312_AR2313_REV8 0x0058 /* AR2313 WMAC (AP43-030) */
  1453. +/* MII registers offset inside Ethernet MMR region */
  1454. +#define AR5312_ENET0_MII_BASE (AR5312_ENET0_BASE + 0x14)
  1455. +#define AR5312_ENET1_MII_BASE (AR5312_ENET1_BASE + 0x14)
  1456. +
  1457. /* Reset/Timer Block Address Map */
  1458. #define AR5312_TIMER 0x0000 /* countdown timer */
  1459. #define AR5312_RELOAD 0x0004 /* timer reload value */
  1460. --- a/arch/mips/ath25/ar2315.c
  1461. +++ b/arch/mips/ath25/ar2315.c
  1462. @@ -132,6 +132,8 @@ static void ar2315_irq_dispatch(void)
  1463. if (pending & CAUSEF_IP3)
  1464. do_IRQ(AR2315_IRQ_WLAN0);
  1465. + else if (pending & CAUSEF_IP4)
  1466. + do_IRQ(AR2315_IRQ_ENET0);
  1467. #ifdef CONFIG_PCI_AR2315
  1468. else if (pending & CAUSEF_IP5)
  1469. do_IRQ(AR2315_IRQ_LCBUS_PCI);
  1470. @@ -167,6 +169,29 @@ void __init ar2315_arch_init_irq(void)
  1471. ar2315_misc_irq_domain = domain;
  1472. }
  1473. +static void ar2315_device_reset_set(u32 mask)
  1474. +{
  1475. + u32 val;
  1476. +
  1477. + val = ar2315_rst_reg_read(AR2315_RESET);
  1478. + ar2315_rst_reg_write(AR2315_RESET, val | mask);
  1479. +}
  1480. +
  1481. +static void ar2315_device_reset_clear(u32 mask)
  1482. +{
  1483. + u32 val;
  1484. +
  1485. + val = ar2315_rst_reg_read(AR2315_RESET);
  1486. + ar2315_rst_reg_write(AR2315_RESET, val & ~mask);
  1487. +}
  1488. +
  1489. +static struct ar231x_eth ar2315_eth_data = {
  1490. + .reset_set = ar2315_device_reset_set,
  1491. + .reset_clear = ar2315_device_reset_clear,
  1492. + .reset_mac = AR2315_RESET_ENET0,
  1493. + .reset_phy = AR2315_RESET_EPHY0,
  1494. +};
  1495. +
  1496. static struct resource ar2315_gpio_res[] = {
  1497. {
  1498. .name = "ar2315-gpio",
  1499. @@ -203,6 +228,11 @@ void __init ar2315_init_devices(void)
  1500. ar2315_gpio_res[1].end = ar2315_gpio_res[1].start;
  1501. platform_device_register(&ar2315_gpio);
  1502. + ar2315_eth_data.macaddr = ath25_board.config->enet0_mac;
  1503. + ath25_add_ethernet(0, AR2315_ENET0_BASE, "eth0_mii",
  1504. + AR2315_ENET0_MII_BASE, AR2315_IRQ_ENET0,
  1505. + &ar2315_eth_data);
  1506. +
  1507. ath25_add_wmac(0, AR2315_WLAN0_BASE, AR2315_IRQ_WLAN0);
  1508. }
  1509. --- a/arch/mips/ath25/ar5312.c
  1510. +++ b/arch/mips/ath25/ar5312.c
  1511. @@ -127,6 +127,10 @@ static void ar5312_irq_dispatch(void)
  1512. if (pending & CAUSEF_IP2)
  1513. do_IRQ(AR5312_IRQ_WLAN0);
  1514. + else if (pending & CAUSEF_IP3)
  1515. + do_IRQ(AR5312_IRQ_ENET0);
  1516. + else if (pending & CAUSEF_IP4)
  1517. + do_IRQ(AR5312_IRQ_ENET1);
  1518. else if (pending & CAUSEF_IP5)
  1519. do_IRQ(AR5312_IRQ_WLAN1);
  1520. else if (pending & CAUSEF_IP6)
  1521. @@ -160,6 +164,36 @@ void __init ar5312_arch_init_irq(void)
  1522. ar5312_misc_irq_domain = domain;
  1523. }
  1524. +static void ar5312_device_reset_set(u32 mask)
  1525. +{
  1526. + u32 val;
  1527. +
  1528. + val = ar5312_rst_reg_read(AR5312_RESET);
  1529. + ar5312_rst_reg_write(AR5312_RESET, val | mask);
  1530. +}
  1531. +
  1532. +static void ar5312_device_reset_clear(u32 mask)
  1533. +{
  1534. + u32 val;
  1535. +
  1536. + val = ar5312_rst_reg_read(AR5312_RESET);
  1537. + ar5312_rst_reg_write(AR5312_RESET, val & ~mask);
  1538. +}
  1539. +
  1540. +static struct ar231x_eth ar5312_eth0_data = {
  1541. + .reset_set = ar5312_device_reset_set,
  1542. + .reset_clear = ar5312_device_reset_clear,
  1543. + .reset_mac = AR5312_RESET_ENET0,
  1544. + .reset_phy = AR5312_RESET_EPHY0,
  1545. +};
  1546. +
  1547. +static struct ar231x_eth ar5312_eth1_data = {
  1548. + .reset_set = ar5312_device_reset_set,
  1549. + .reset_clear = ar5312_device_reset_clear,
  1550. + .reset_mac = AR5312_RESET_ENET1,
  1551. + .reset_phy = AR5312_RESET_EPHY1,
  1552. +};
  1553. +
  1554. static struct physmap_flash_data ar5312_flash_data = {
  1555. .width = 2,
  1556. };
  1557. @@ -240,6 +274,7 @@ static void __init ar5312_flash_init(voi
  1558. void __init ar5312_init_devices(void)
  1559. {
  1560. struct ath25_boarddata *config;
  1561. + u8 *c;
  1562. ar5312_flash_init();
  1563. @@ -263,8 +298,30 @@ void __init ar5312_init_devices(void)
  1564. platform_device_register(&ar5312_gpio);
  1565. + /* Fix up MAC addresses if necessary */
  1566. + if (is_broadcast_ether_addr(config->enet0_mac))
  1567. + ether_addr_copy(config->enet0_mac, config->enet1_mac);
  1568. +
  1569. + /* If ENET0 and ENET1 have the same mac address,
  1570. + * increment the one from ENET1 */
  1571. + if (ether_addr_equal(config->enet0_mac, config->enet1_mac)) {
  1572. + c = config->enet1_mac + 5;
  1573. + while ((c >= config->enet1_mac) && !(++(*c)))
  1574. + c--;
  1575. + }
  1576. +
  1577. switch (ath25_soc) {
  1578. case ATH25_SOC_AR5312:
  1579. + ar5312_eth0_data.macaddr = config->enet0_mac;
  1580. + ath25_add_ethernet(0, AR5312_ENET0_BASE, "eth0_mii",
  1581. + AR5312_ENET0_MII_BASE, AR5312_IRQ_ENET0,
  1582. + &ar5312_eth0_data);
  1583. +
  1584. + ar5312_eth1_data.macaddr = config->enet1_mac;
  1585. + ath25_add_ethernet(1, AR5312_ENET1_BASE, "eth1_mii",
  1586. + AR5312_ENET1_MII_BASE, AR5312_IRQ_ENET1,
  1587. + &ar5312_eth1_data);
  1588. +
  1589. if (!ath25_board.radio)
  1590. return;
  1591. @@ -273,8 +330,18 @@ void __init ar5312_init_devices(void)
  1592. ath25_add_wmac(0, AR5312_WLAN0_BASE, AR5312_IRQ_WLAN0);
  1593. break;
  1594. + /*
  1595. + * AR2312/3 ethernet uses the PHY of ENET0, but the MAC
  1596. + * of ENET1. Atheros calls it 'twisted' for a reason :)
  1597. + */
  1598. case ATH25_SOC_AR2312:
  1599. case ATH25_SOC_AR2313:
  1600. + ar5312_eth1_data.reset_phy = ar5312_eth0_data.reset_phy;
  1601. + ar5312_eth1_data.macaddr = config->enet0_mac;
  1602. + ath25_add_ethernet(1, AR5312_ENET1_BASE, "eth0_mii",
  1603. + AR5312_ENET0_MII_BASE, AR5312_IRQ_ENET1,
  1604. + &ar5312_eth1_data);
  1605. +
  1606. if (!ath25_board.radio)
  1607. return;
  1608. break;
  1609. --- a/arch/mips/ath25/devices.h
  1610. +++ b/arch/mips/ath25/devices.h
  1611. @@ -33,6 +33,8 @@ extern struct ar231x_board_config ath25_
  1612. extern void (*ath25_irq_dispatch)(void);
  1613. int ath25_find_config(phys_addr_t offset, unsigned long size);
  1614. +int ath25_add_ethernet(int nr, u32 base, const char *mii_name, u32 mii_base,
  1615. + int irq, void *pdata);
  1616. void ath25_serial_setup(u32 mapbase, int irq, unsigned int uartclk);
  1617. int ath25_add_wmac(int nr, u32 base, int irq);
  1618. --- a/arch/mips/ath25/devices.c
  1619. +++ b/arch/mips/ath25/devices.c
  1620. @@ -13,6 +13,51 @@
  1621. struct ar231x_board_config ath25_board;
  1622. enum ath25_soc_type ath25_soc = ATH25_SOC_UNKNOWN;
  1623. +static struct resource ath25_eth0_res[] = {
  1624. + {
  1625. + .name = "eth0_membase",
  1626. + .flags = IORESOURCE_MEM,
  1627. + },
  1628. + {
  1629. + .name = "eth0_mii",
  1630. + .flags = IORESOURCE_MEM,
  1631. + },
  1632. + {
  1633. + .name = "eth0_irq",
  1634. + .flags = IORESOURCE_IRQ,
  1635. + }
  1636. +};
  1637. +
  1638. +static struct resource ath25_eth1_res[] = {
  1639. + {
  1640. + .name = "eth1_membase",
  1641. + .flags = IORESOURCE_MEM,
  1642. + },
  1643. + {
  1644. + .name = "eth1_mii",
  1645. + .flags = IORESOURCE_MEM,
  1646. + },
  1647. + {
  1648. + .name = "eth1_irq",
  1649. + .flags = IORESOURCE_IRQ,
  1650. + }
  1651. +};
  1652. +
  1653. +static struct platform_device ath25_eth[] = {
  1654. + {
  1655. + .id = 0,
  1656. + .name = "ar231x-eth",
  1657. + .resource = ath25_eth0_res,
  1658. + .num_resources = ARRAY_SIZE(ath25_eth0_res)
  1659. + },
  1660. + {
  1661. + .id = 1,
  1662. + .name = "ar231x-eth",
  1663. + .resource = ath25_eth1_res,
  1664. + .num_resources = ARRAY_SIZE(ath25_eth1_res)
  1665. + }
  1666. +};
  1667. +
  1668. static struct resource ath25_wmac0_res[] = {
  1669. {
  1670. .name = "wmac0_membase",
  1671. @@ -71,6 +116,25 @@ const char *get_system_type(void)
  1672. return soc_type_strings[ath25_soc];
  1673. }
  1674. +int __init ath25_add_ethernet(int nr, u32 base, const char *mii_name,
  1675. + u32 mii_base, int irq, void *pdata)
  1676. +{
  1677. + struct resource *res;
  1678. +
  1679. + ath25_eth[nr].dev.platform_data = pdata;
  1680. + res = &ath25_eth[nr].resource[0];
  1681. + res->start = base;
  1682. + res->end = base + 0x2000 - 1;
  1683. + res++;
  1684. + res->name = mii_name;
  1685. + res->start = mii_base;
  1686. + res->end = mii_base + 8 - 1;
  1687. + res++;
  1688. + res->start = irq;
  1689. + res->end = irq;
  1690. + return platform_device_register(&ath25_eth[nr]);
  1691. +}
  1692. +
  1693. void __init ath25_serial_setup(u32 mapbase, int irq, unsigned int uartclk)
  1694. {
  1695. #ifdef CONFIG_SERIAL_8250_CONSOLE
  1696. --- a/arch/mips/include/asm/mach-ath25/ath25_platform.h
  1697. +++ b/arch/mips/include/asm/mach-ath25/ath25_platform.h
  1698. @@ -71,4 +71,15 @@ struct ar231x_board_config {
  1699. const char *radio;
  1700. };
  1701. +/*
  1702. + * Platform device information for the Ethernet MAC
  1703. + */
  1704. +struct ar231x_eth {
  1705. + void (*reset_set)(u32);
  1706. + void (*reset_clear)(u32);
  1707. + u32 reset_mac;
  1708. + u32 reset_phy;
  1709. + char *macaddr;
  1710. +};
  1711. +
  1712. #endif /* __ASM_MACH_ATH25_PLATFORM_H */