davinci_emac.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /*
  2. * File : davinci_emac.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-01-13 weety first version
  23. */
  24. #include <rtthread.h>
  25. #include <netif/ethernetif.h>
  26. #include <lwipopts.h>
  27. #include <dm36x.h>
  28. #include "davinci_emac.h"
  29. #define MMU_NOCACHE_ADDR(a) ((rt_uint32_t)a | (1UL<<29))
  30. #define CACHE_LINE_SIZE 32
  31. extern void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size);
  32. extern void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size);
  33. /* EMAC internal utility function */
  34. static inline rt_uint32_t emac_virt_to_phys(void *addr)
  35. {
  36. return addr;
  37. }
  38. static inline rt_uint32_t virt_to_phys(void *addr)
  39. {
  40. return addr;
  41. }
  42. /* Cache macros - Packet buffers would be from pbuf pool which is cached */
  43. #define EMAC_VIRT_NOCACHE(addr) (addr)
  44. #define EMAC_CACHE_INVALIDATE(addr, size) \
  45. mmu_invalidate_dcache(addr, size)
  46. #define EMAC_CACHE_WRITEBACK(addr, size) \
  47. mmu_clean_dcache(addr, size)
  48. #define EMAC_CACHE_WRITEBACK_INVALIDATE(addr, size) \
  49. mmu_clean_invalidated_dcache(addr, size)
  50. /* DM644x does not have BD's in cached memory - so no cache functions */
  51. #define BD_CACHE_INVALIDATE(addr, size)
  52. #define BD_CACHE_WRITEBACK(addr, size)
  53. #define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
  54. static struct emac_priv davinci_emac_device;
  55. static struct rt_semaphore sem_ack;
  56. /* clock frequency for EMAC */
  57. static unsigned long emac_bus_frequency;
  58. static unsigned long mdio_max_freq;
  59. #define EMAC_AUTONEG_TIMEOUT 5000000
  60. #define EMAC_LINK_TIMEOUT 500000
  61. /* EMAC TX Host Error description strings */
  62. static char *emac_txhost_errcodes[16] = {
  63. "No error", "SOP error", "Ownership bit not set in SOP buffer",
  64. "Zero Next Buffer Descriptor Pointer Without EOP",
  65. "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
  66. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  67. "Reserved", "Reserved", "Reserved", "Reserved"
  68. };
  69. /* EMAC RX Host Error description strings */
  70. static char *emac_rxhost_errcodes[16] = {
  71. "No error", "Reserved", "Ownership bit not set in input buffer",
  72. "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
  73. "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
  74. "Reserved", "Reserved", "Reserved", "Reserved"
  75. };
  76. #define emac_read(reg) davinci_readl(priv->emac_base + (reg))
  77. #define emac_write(reg, val) davinci_writel(val, priv->emac_base + (reg))
  78. #define emac_ctrl_read(reg) davinci_readl((priv->ctrl_base + (reg)))
  79. #define emac_ctrl_write(reg, val) davinci_writel(val, (priv->ctrl_base + (reg)))
  80. #define emac_mdio_read(reg) davinci_readl(priv->mdio_base + (reg))
  81. #define emac_mdio_write(reg, val) davinci_writel(val, (priv->mdio_base + (reg)))
  82. static void emac_int_enable(struct emac_priv *priv);
  83. static void emac_int_disable(struct emac_priv *priv);
  84. static int emac_init_txch(struct emac_priv *priv, rt_uint32_t ch);
  85. /* PHY/MII bus related */
  86. /* Wait until mdio is ready for next command */
  87. #define MDIO_WAIT_FOR_USER_ACCESS\
  88. while ((emac_mdio_read((MDIO_USERACCESS(0))) &\
  89. MDIO_USERACCESS_GO) != 0)
  90. static int emac_mii_read(struct emac_priv *priv, int phy_id, int phy_reg)
  91. {
  92. unsigned int phy_data = 0;
  93. unsigned int phy_control;
  94. /* Wait until mdio is ready for next command */
  95. MDIO_WAIT_FOR_USER_ACCESS;
  96. phy_control = (MDIO_USERACCESS_GO |
  97. MDIO_USERACCESS_READ |
  98. ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
  99. ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
  100. (phy_data & MDIO_USERACCESS_DATA));
  101. emac_mdio_write(MDIO_USERACCESS(0), phy_control);
  102. /* Wait until mdio is ready for next command */
  103. MDIO_WAIT_FOR_USER_ACCESS;
  104. return emac_mdio_read(MDIO_USERACCESS(0)) & MDIO_USERACCESS_DATA;
  105. }
  106. static int emac_mii_write(struct emac_priv *priv, int phy_id,
  107. int phy_reg, rt_uint16_t phy_data)
  108. {
  109. unsigned int control;
  110. /* until mdio is ready for next command */
  111. MDIO_WAIT_FOR_USER_ACCESS;
  112. control = (MDIO_USERACCESS_GO |
  113. MDIO_USERACCESS_WRITE |
  114. ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
  115. ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
  116. (phy_data & MDIO_USERACCESS_DATA));
  117. emac_mdio_write(MDIO_USERACCESS(0), control);
  118. return 0;
  119. }
  120. static int emac_mii_reset(struct emac_priv *priv)
  121. {
  122. unsigned int clk_div;
  123. int mdio_bus_freq = emac_bus_frequency;
  124. if (mdio_max_freq && mdio_bus_freq)
  125. clk_div = ((mdio_bus_freq / mdio_max_freq) - 1);
  126. else
  127. clk_div = 0xFF;
  128. clk_div &= MDIO_CONTROL_CLKDIV;
  129. /* Set enable and clock divider in MDIOControl */
  130. emac_mdio_write(MDIO_CONTROL, (clk_div | MDIO_CONTROL_ENABLE));
  131. return 0;
  132. }
  133. /* Duplex, half or full. */
  134. #define DUPLEX_HALF 0x00
  135. #define DUPLEX_FULL 0x01
  136. static void udelay(rt_uint32_t us)
  137. {
  138. rt_uint32_t len;
  139. for (;us > 0; us --)
  140. for (len = 0; len < 10; len++ );
  141. }
  142. static void davinci_emac_phy_reset(rt_device_t dev)
  143. {
  144. int i;
  145. rt_uint16_t status, adv;
  146. struct emac_priv *priv = dev->user_data;;
  147. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  148. emac_mii_write(priv, priv->phy_addr, MII_ADVERTISE, adv);
  149. rt_kprintf("%s: Starting autonegotiation...\n", dev->parent.name);
  150. emac_mii_write(priv, priv->phy_addr, MII_BMCR, (BMCR_ANENABLE
  151. | BMCR_ANRESTART));
  152. for (i = 0; i < EMAC_AUTONEG_TIMEOUT / 100; i++) {
  153. status = emac_mii_read(priv, priv->phy_addr, MII_BMSR);
  154. if (status & BMSR_ANEGCOMPLETE)
  155. break;
  156. udelay(100);
  157. }
  158. if (status & BMSR_ANEGCOMPLETE)
  159. rt_kprintf("%s: Autonegotiation complete\n", dev->parent.name);
  160. else
  161. rt_kprintf("%s: Autonegotiation timed out (status=0x%04x)\n",
  162. dev->parent.name, status);
  163. }
  164. static int davinci_emac_phy_init(rt_device_t dev)
  165. {
  166. struct emac_priv *priv = dev->user_data;
  167. rt_uint16_t phy_id, status, adv, lpa;
  168. int media, speed, duplex;
  169. int i;
  170. /* Check if the PHY is up to snuff... */
  171. phy_id = emac_mii_read(priv, priv->phy_addr, MII_PHYSID1);
  172. if (phy_id == 0xffff) {
  173. rt_kprintf("%s: No PHY present\n", dev->parent.name);
  174. return 0;
  175. }
  176. status = emac_mii_read(priv, priv->phy_addr, MII_BMSR);
  177. if (!(status & BMSR_LSTATUS)) {
  178. /* Try to re-negotiate if we don't have link already. */
  179. davinci_emac_phy_reset(dev);
  180. for (i = 0; i < EMAC_LINK_TIMEOUT / 100; i++) {
  181. status = emac_mii_read(priv, priv->phy_addr, MII_BMSR);
  182. if (status & BMSR_LSTATUS)
  183. break;
  184. udelay(100);
  185. }
  186. }
  187. if (!(status & BMSR_LSTATUS)) {
  188. rt_kprintf("%s: link down (status: 0x%04x)\n",
  189. dev->parent.name, status);
  190. priv->link = 0;
  191. return 0;
  192. } else {
  193. adv = emac_mii_read(priv, priv->phy_addr, MII_ADVERTISE);
  194. lpa = emac_mii_read(priv, priv->phy_addr, MII_LPA);
  195. media = mii_nway_result(lpa & adv);
  196. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  197. ? 1 : 0);
  198. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  199. rt_kprintf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  200. dev->parent.name,
  201. speed ? "100" : "10",
  202. duplex ? "full" : "half",
  203. lpa);
  204. priv->speed = speed;
  205. priv->duplex = duplex;
  206. priv->link = 1;
  207. return 1;
  208. }
  209. }
  210. /**
  211. * emac_update_phystatus: Update Phy status
  212. * @priv: The DaVinci EMAC driver private structure
  213. *
  214. * Updates phy status and takes action for network queue if required
  215. * based upon link status
  216. *
  217. */
  218. static void emac_update_phystatus(struct emac_priv *priv)
  219. {
  220. rt_uint32_t mac_control;
  221. rt_uint32_t new_duplex;
  222. rt_uint32_t cur_duplex;
  223. mac_control = emac_read(EMAC_MACCONTROL);
  224. cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
  225. DUPLEX_FULL : DUPLEX_HALF;
  226. if (priv->phy_mask)
  227. new_duplex = priv->duplex;
  228. else
  229. new_duplex = DUPLEX_FULL;
  230. /* We get called only if link has changed (speed/duplex/status) */
  231. if ((priv->link) && (new_duplex != cur_duplex)) {
  232. priv->duplex = new_duplex;
  233. if (DUPLEX_FULL == priv->duplex)
  234. mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
  235. else
  236. mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
  237. }
  238. if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
  239. mac_control = emac_read(EMAC_MACCONTROL);
  240. mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
  241. EMAC_DM646X_MACCONTORL_GIGFORCE);
  242. } else {
  243. /* Clear the GIG bit and GIGFORCE bit */
  244. mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
  245. EMAC_DM646X_MACCONTORL_GIG);
  246. if (priv->rmii_en && (priv->speed == SPEED_100))
  247. mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
  248. else
  249. mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
  250. }
  251. /* Update mac_control if changed */
  252. emac_write(EMAC_MACCONTROL, mac_control);
  253. #if 0
  254. if (priv->link) {
  255. /* link ON */
  256. /* reactivate the transmit queue if it is stopped */
  257. } else {
  258. /* link OFF */
  259. }
  260. #endif
  261. }
  262. void davinci_emac_update_link(void *param)
  263. {
  264. struct emac_priv *priv = param;
  265. rt_device_t dev = &(priv->parent.parent);
  266. rt_uint32_t status, status_change = 0;
  267. rt_uint32_t link;
  268. rt_uint32_t media;
  269. rt_uint16_t adv, lpa;
  270. status = emac_mii_read(priv, priv->phy_addr, MII_BMSR);
  271. if ((status & BMSR_LSTATUS) == 0)
  272. link = 0;
  273. else
  274. link = 1;
  275. if (link != priv->link) {
  276. priv->link = link;
  277. status_change = 1;
  278. }
  279. if (status_change) {
  280. if (priv->link) {
  281. adv = emac_mii_read(priv, priv->phy_addr, MII_ADVERTISE);
  282. lpa = emac_mii_read(priv, priv->phy_addr, MII_LPA);
  283. media = mii_nway_result(lpa & adv);
  284. priv->speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  285. ? 100 : 10);
  286. priv->duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  287. rt_kprintf("%s: link up (%dMbps/%s-duplex)\n",
  288. dev->parent.name, priv->speed,
  289. DUPLEX_FULL == priv->duplex ? "Full":"Half");
  290. eth_device_linkchange(&priv->parent, RT_TRUE);
  291. } else {
  292. rt_kprintf("%s: link down\n", dev->parent.name);
  293. eth_device_linkchange(&priv->parent, RT_FALSE);
  294. }
  295. emac_update_phystatus(priv);
  296. }
  297. }
  298. /**
  299. * emac_net_tx_complete: TX packet completion function
  300. * @priv: The DaVinci EMAC driver private structure
  301. * @net_data_tokens: packet token - pbuf pointer
  302. * @num_tokens: number of pbuf's to free
  303. * @ch: TX channel number
  304. *
  305. * Frees the pbuf once packet is transmitted
  306. *
  307. */
  308. static int emac_net_tx_complete(struct emac_priv *priv,
  309. void **net_data_tokens,
  310. int num_tokens, rt_uint32_t ch)
  311. {
  312. rt_uint32_t cnt;
  313. for (cnt = 0; cnt < num_tokens; cnt++) {
  314. struct pbuf *p = (struct pbuf *)net_data_tokens[cnt];
  315. if (p == RT_NULL)
  316. continue;
  317. priv->net_dev_stats.tx_packets++;
  318. priv->net_dev_stats.tx_bytes += p->len;
  319. //free pbuf
  320. }
  321. rt_sem_release(&sem_ack);
  322. return 0;
  323. }
  324. /**
  325. * emac_txch_teardown: TX channel teardown
  326. * @priv: The DaVinci EMAC driver private structure
  327. * @ch: TX channel number
  328. *
  329. * Called to teardown TX channel
  330. *
  331. */
  332. static void emac_txch_teardown(struct emac_priv *priv, rt_uint32_t ch)
  333. {
  334. rt_uint32_t teardown_cnt = 0xFFFFFFF0; /* Some high value */
  335. struct emac_txch *txch = priv->txch[ch];
  336. struct emac_tx_bd __iomem *curr_bd;
  337. while ((emac_read(EMAC_TXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
  338. EMAC_TEARDOWN_VALUE) {
  339. /* wait till tx teardown complete */
  340. --teardown_cnt;
  341. if (0 == teardown_cnt) {
  342. rt_kprintf("EMAC: TX teardown aborted\n");
  343. break;
  344. }
  345. }
  346. emac_write(EMAC_TXCP(ch), EMAC_TEARDOWN_VALUE);
  347. /* process sent packets and return pbuf's to upper layer */
  348. if (1 == txch->queue_active) {
  349. curr_bd = txch->active_queue_head;
  350. while (curr_bd != RT_NULL) {
  351. emac_net_tx_complete(priv, (void *)
  352. &curr_bd->buf_token, 1, ch);
  353. if (curr_bd != txch->active_queue_tail)
  354. curr_bd = curr_bd->next;
  355. else
  356. break;
  357. }
  358. txch->bd_pool_head = txch->active_queue_head;
  359. txch->active_queue_head =
  360. txch->active_queue_tail = RT_NULL;
  361. }
  362. }
  363. /**
  364. * emac_stop_txch: Stop TX channel operation
  365. * @priv: The DaVinci EMAC driver private structure
  366. * @ch: TX channel number
  367. *
  368. * Called to stop TX channel operation
  369. *
  370. */
  371. static void emac_stop_txch(struct emac_priv *priv, rt_uint32_t ch)
  372. {
  373. struct emac_txch *txch = priv->txch[ch];
  374. if (txch) {
  375. txch->teardown_pending = 1;
  376. emac_write(EMAC_TXTEARDOWN, 0);
  377. emac_txch_teardown(priv, ch);
  378. txch->teardown_pending = 0;
  379. emac_write(EMAC_TXINTMASKCLEAR, BIT(ch));
  380. }
  381. }
  382. /**
  383. * emac_tx_bdproc: TX buffer descriptor (packet) processing
  384. * @priv: The DaVinci EMAC driver private structure
  385. * @ch: TX channel number to process buffer descriptors for
  386. * @budget: number of packets allowed to process
  387. * @pending: indication to caller that packets are pending to process
  388. *
  389. * Processes TX buffer descriptors after packets are transmitted - checks
  390. * ownership bit on the TX * descriptor and requeues it to free pool & frees
  391. * the PBUF buffer. Only "budget" number of packets are processed and
  392. * indication of pending packets provided to the caller
  393. *
  394. * Returns number of packets processed
  395. */
  396. static int emac_tx_bdproc(struct emac_priv *priv, rt_uint32_t ch, rt_uint32_t budget)
  397. {
  398. unsigned long flags;
  399. rt_uint32_t frame_status;
  400. rt_uint32_t pkts_processed = 0;
  401. rt_uint32_t tx_complete_cnt = 0;
  402. struct emac_tx_bd __iomem *curr_bd;
  403. struct emac_txch *txch = priv->txch[ch];
  404. rt_uint32_t *tx_complete_ptr = txch->tx_complete;
  405. if (1 == txch->teardown_pending) {
  406. rt_kprintf("DaVinci EMAC:emac_tx_bdproc: "\
  407. "teardown pending\n");
  408. return 0; /* dont handle any pkt completions */
  409. }
  410. ++txch->proc_count;
  411. rt_sem_take(&priv->tx_lock, RT_WAITING_FOREVER);
  412. curr_bd = txch->active_queue_head;
  413. if (RT_NULL == curr_bd) {
  414. emac_write(EMAC_TXCP(ch),
  415. emac_virt_to_phys(txch->last_hw_bdprocessed));
  416. txch->no_active_pkts++;
  417. rt_sem_release(&priv->tx_lock);
  418. return 0;
  419. }
  420. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  421. frame_status = curr_bd->mode;
  422. while ((curr_bd) &&
  423. ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
  424. (pkts_processed < budget)) {
  425. emac_write(EMAC_TXCP(ch), emac_virt_to_phys(curr_bd));
  426. txch->active_queue_head = curr_bd->next;
  427. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  428. if (curr_bd->next) { /* misqueued packet */
  429. emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
  430. ++txch->mis_queued_packets;
  431. } else {
  432. txch->queue_active = 0; /* end of queue */
  433. }
  434. }
  435. *tx_complete_ptr = (rt_uint32_t) curr_bd->buf_token;
  436. ++tx_complete_ptr;
  437. ++tx_complete_cnt;
  438. curr_bd->next = txch->bd_pool_head;
  439. txch->bd_pool_head = curr_bd;
  440. --txch->active_queue_count;
  441. pkts_processed++;
  442. txch->last_hw_bdprocessed = curr_bd;
  443. curr_bd = txch->active_queue_head;
  444. if (curr_bd) {
  445. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  446. frame_status = curr_bd->mode;
  447. }
  448. } /* end of pkt processing loop */
  449. emac_net_tx_complete(priv,
  450. (void *)&txch->tx_complete[0],
  451. tx_complete_cnt, ch);
  452. rt_sem_release(&priv->tx_lock);
  453. return pkts_processed;
  454. }
  455. #define EMAC_ERR_TX_OUT_OF_BD -1
  456. /**
  457. * emac_send: EMAC Transmit function (internal)
  458. * @priv: The DaVinci EMAC driver private structure
  459. * @pkt: packet pointer (contains pbuf ptr)
  460. * @ch: TX channel number
  461. *
  462. * Called by the transmit function to queue the packet in EMAC hardware queue
  463. *
  464. * Returns success(0) or error code (typically out of desc's)
  465. */
  466. static int emac_send(struct emac_priv *priv, struct emac_netpktobj *pkt, rt_uint32_t ch)
  467. {
  468. unsigned long flags;
  469. struct emac_tx_bd __iomem *curr_bd;
  470. struct emac_txch *txch;
  471. struct emac_netbufobj *buf_list;
  472. txch = priv->txch[ch];
  473. buf_list = pkt->buf_list; /* get handle to the buffer array */
  474. /* check packet size and pad if short */
  475. if (pkt->pkt_length < EMAC_DEF_MIN_ETHPKTSIZE) {
  476. buf_list->length += (EMAC_DEF_MIN_ETHPKTSIZE - pkt->pkt_length);
  477. pkt->pkt_length = EMAC_DEF_MIN_ETHPKTSIZE;
  478. }
  479. rt_sem_take(&priv->tx_lock, RT_WAITING_FOREVER);
  480. curr_bd = txch->bd_pool_head;
  481. if (curr_bd == RT_NULL) {
  482. txch->out_of_tx_bd++;
  483. rt_sem_release(&priv->tx_lock);
  484. return EMAC_ERR_TX_OUT_OF_BD;
  485. }
  486. txch->bd_pool_head = curr_bd->next;
  487. curr_bd->buf_token = buf_list->buf_token;
  488. curr_bd->buff_ptr = virt_to_phys(buf_list->data_ptr);
  489. curr_bd->off_b_len = buf_list->length;
  490. curr_bd->h_next = 0;
  491. curr_bd->next = RT_NULL;
  492. curr_bd->mode = (EMAC_CPPI_SOP_BIT | EMAC_CPPI_OWNERSHIP_BIT |
  493. EMAC_CPPI_EOP_BIT | pkt->pkt_length);
  494. /* flush the packet from cache if write back cache is present */
  495. BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  496. /* send the packet */
  497. if (txch->active_queue_head == RT_NULL) {
  498. txch->active_queue_head = curr_bd;
  499. txch->active_queue_tail = curr_bd;
  500. if (1 != txch->queue_active) {
  501. emac_write(EMAC_TXHDP(ch),
  502. emac_virt_to_phys(curr_bd));
  503. txch->queue_active = 1;
  504. }
  505. ++txch->queue_reinit;
  506. } else {
  507. register struct emac_tx_bd __iomem *tail_bd;
  508. register rt_uint32_t frame_status;
  509. tail_bd = txch->active_queue_tail;
  510. tail_bd->next = curr_bd;
  511. txch->active_queue_tail = curr_bd;
  512. tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
  513. tail_bd->h_next = (int)emac_virt_to_phys(curr_bd);
  514. frame_status = tail_bd->mode;
  515. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  516. emac_write(EMAC_TXHDP(ch), emac_virt_to_phys(curr_bd));
  517. frame_status &= ~(EMAC_CPPI_EOQ_BIT);
  518. tail_bd->mode = frame_status;
  519. ++txch->end_of_queue_add;
  520. }
  521. }
  522. txch->active_queue_count++;
  523. rt_sem_release(&priv->tx_lock);
  524. return 0;
  525. }
  526. /**
  527. * emac_dev_xmit: EMAC Transmit function
  528. * @pbuf: PBUF pointer
  529. * @priv: The DaVinci EMAC driver private structure
  530. *
  531. * Called by the system to transmit a packet - we queue the packet in
  532. * EMAC hardware transmit queue
  533. *
  534. * Returns success(RT_EOK) or error code (typically out of desc's)
  535. */
  536. static int emac_dev_xmit(struct pbuf *p, struct emac_priv *priv)
  537. {
  538. int ret_code;
  539. struct emac_netbufobj tx_buf; /* buffer obj-only single frame support */
  540. struct emac_netpktobj tx_packet; /* packet object */
  541. /* If no link, return */
  542. if (!priv->link) {
  543. rt_kprintf("DaVinci EMAC: No link to transmit\n");
  544. return -RT_EBUSY;
  545. }
  546. /* Build the buffer and packet objects - Since only single fragment is
  547. * supported, need not set length and token in both packet & object.
  548. * Doing so for completeness sake & to show that this needs to be done
  549. * in multifragment case
  550. */
  551. tx_packet.buf_list = &tx_buf;
  552. tx_packet.num_bufs = 1; /* only single fragment supported */
  553. tx_packet.pkt_length = p->len;
  554. tx_packet.pkt_token = (void *)p;
  555. tx_buf.length = p->len;
  556. tx_buf.buf_token = (void *)p;
  557. tx_buf.data_ptr = p->payload;
  558. EMAC_CACHE_WRITEBACK((unsigned long)p->payload, p->len);
  559. ret_code = emac_send(priv, &tx_packet, EMAC_DEF_TX_CH);
  560. if (ret_code != 0) {
  561. if (ret_code == EMAC_ERR_TX_OUT_OF_BD) {
  562. rt_kprintf("DaVinci EMAC: xmit() fatal"\
  563. " err. Out of TX BD's\n");
  564. }
  565. priv->net_dev_stats.tx_dropped++;
  566. return -RT_EBUSY;
  567. }
  568. return RT_EOK;
  569. }
  570. /**
  571. * emac_cleanup_txch: Book-keep function to clean TX channel resources
  572. * @priv: The DaVinci EMAC private adapter structure
  573. * @ch: TX channel number
  574. *
  575. * Called to clean up TX channel resources
  576. *
  577. */
  578. static void emac_cleanup_txch(struct emac_priv *priv, rt_uint32_t ch)
  579. {
  580. struct emac_txch *txch = priv->txch[ch];
  581. if (txch) {
  582. if (txch->bd_mem)
  583. txch->bd_mem = NULL;
  584. rt_free(txch->tx_complete);
  585. rt_free(txch);
  586. priv->txch[ch] = NULL;
  587. }
  588. }
  589. /**
  590. * emac_dev_tx_timeout: EMAC Transmit timeout function
  591. * @ndev: The DaVinci EMAC network adapter
  592. *
  593. * Called when system detects that a skb timeout period has expired
  594. * potentially due to a fault in the adapter in not being able to send
  595. * it out on the wire. We teardown the TX channel assuming a hardware
  596. * error and re-initialize the TX channel for hardware operation
  597. *
  598. */
  599. static void emac_dev_tx_timeout(struct emac_priv *priv)
  600. {
  601. rt_kprintf("emac tx timeout.\n");
  602. priv->net_dev_stats.tx_errors++;
  603. emac_int_disable(priv);
  604. emac_stop_txch(priv, EMAC_DEF_TX_CH);
  605. emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
  606. emac_init_txch(priv, EMAC_DEF_TX_CH);
  607. emac_write(EMAC_TXHDP(0), 0);
  608. emac_write(EMAC_TXINTMASKSET, BIT(EMAC_DEF_TX_CH));
  609. emac_int_enable(priv);
  610. }
  611. /* ethernet device interface */
  612. /* transmit packet. */
  613. rt_err_t rt_davinci_emac_tx( rt_device_t dev, struct pbuf* p)
  614. {
  615. /*struct pbuf* q;
  616. rt_uint8_t* bufptr, *buf = RT_NULL;
  617. unsigned long ctrl;
  618. rt_uint32_t addr;*/
  619. rt_err_t err;
  620. struct emac_priv *priv = dev->user_data;
  621. emac_dev_xmit(p, priv);
  622. #if 0
  623. buf = rt_malloc(p->tot_len);
  624. if (!buf) {
  625. rt_kprintf("%s:alloc buf failed\n", __func__);
  626. return -RT_ENOMEM;
  627. }
  628. bufptr = buf;
  629. /*for (q = p; q != RT_NULL; q = q->next)
  630. {
  631. memcpy(bufptr, q->payload, q->len);
  632. bufptr += q->len;
  633. }*/
  634. #endif
  635. /* wait ack */
  636. err = rt_sem_take(&sem_ack, RT_TICK_PER_SECOND*5);
  637. if (err != RT_EOK)
  638. {
  639. emac_dev_tx_timeout(priv);
  640. }
  641. //rt_free(buf);
  642. return RT_EOK;
  643. }
  644. /**
  645. * emac_addbd_to_rx_queue: Recycle RX buffer descriptor
  646. * @priv: The DaVinci EMAC driver private structure
  647. * @ch: RX channel number to process buffer descriptors for
  648. * @curr_bd: current buffer descriptor
  649. * @buffer: buffer pointer for descriptor
  650. * @buf_token: buffer token (stores pbuf information)
  651. *
  652. * Prepares the recycled buffer descriptor and addes it to hardware
  653. * receive queue - if queue empty this descriptor becomes the head
  654. * else addes the descriptor to end of queue
  655. *
  656. */
  657. static void emac_addbd_to_rx_queue(struct emac_priv *priv, rt_uint32_t ch,
  658. struct emac_rx_bd __iomem *curr_bd,
  659. char *buffer, void *buf_token)
  660. {
  661. struct emac_rxch *rxch = priv->rxch[ch];
  662. /* populate the hardware descriptor */
  663. curr_bd->h_next = 0;
  664. curr_bd->buff_ptr = virt_to_phys(buffer);
  665. curr_bd->off_b_len = rxch->buf_size;
  666. curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
  667. curr_bd->next = RT_NULL;
  668. curr_bd->data_ptr = buffer;
  669. curr_bd->buf_token = buf_token;
  670. /* write back */
  671. BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  672. if (rxch->active_queue_head == RT_NULL) {
  673. rxch->active_queue_head = curr_bd;
  674. rxch->active_queue_tail = curr_bd;
  675. if (0 != rxch->queue_active) {
  676. emac_write(EMAC_RXHDP(ch),
  677. emac_virt_to_phys(rxch->active_queue_head));
  678. rxch->queue_active = 1;
  679. }
  680. } else {
  681. struct emac_rx_bd __iomem *tail_bd;
  682. rt_uint32_t frame_status;
  683. tail_bd = rxch->active_queue_tail;
  684. rxch->active_queue_tail = curr_bd;
  685. tail_bd->next = curr_bd;
  686. tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
  687. tail_bd->h_next = emac_virt_to_phys(curr_bd);
  688. frame_status = tail_bd->mode;
  689. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  690. emac_write(EMAC_RXHDP(ch),
  691. emac_virt_to_phys(curr_bd));
  692. frame_status &= ~(EMAC_CPPI_EOQ_BIT);
  693. tail_bd->mode = frame_status;
  694. ++rxch->end_of_queue_add;
  695. }
  696. }
  697. ++rxch->recycled_bd;
  698. }
  699. /**
  700. * emac_net_rx_cb: Prepares packet and sends to upper layer
  701. * @priv: The DaVinci EMAC driver private structure
  702. * @net_pkt_list: Network packet list (received packets)
  703. *
  704. * Invalidates packet buffer memory and sends the received packet to upper
  705. * layer
  706. *
  707. * Returns success or appropriate error code (none as of now)
  708. */
  709. static int emac_net_rx_cb(struct emac_priv *priv,
  710. struct emac_netpktobj *net_pkt_list)
  711. {
  712. struct eth_device *device = &priv->parent;
  713. struct pbuf *p;
  714. p = (struct pbuf *)net_pkt_list->pkt_token;
  715. /* set length of packet */
  716. p->tot_len = net_pkt_list->pkt_length;
  717. p->len = net_pkt_list->pkt_length;
  718. EMAC_CACHE_INVALIDATE((unsigned long)p->payload, p->len);
  719. if (device->netif->input(p, device->netif) != RT_EOK)
  720. {
  721. pbuf_free(p);
  722. }
  723. priv->net_dev_stats.rx_bytes += net_pkt_list->pkt_length;
  724. priv->net_dev_stats.rx_packets++;
  725. return 0;
  726. }
  727. /**
  728. * emac_net_alloc_rx_buf: Allocate a pbuf for RX
  729. * @priv: The DaVinci EMAC driver private structure
  730. * @buf_size: size of PBUF data buffer to allocate
  731. * @data_token: data token returned (pbuf handle for storing in buffer desc)
  732. * @ch: RX channel number
  733. *
  734. * Called during RX channel setup - allocates pbuf buffer of required size
  735. * and provides the pbuf handle and allocated buffer data pointer to caller
  736. *
  737. * Returns pbuf data pointer or 0 on failure to alloc pbuf
  738. */
  739. static void *emac_net_alloc_rx_buf(struct emac_priv *priv, int buf_size,
  740. void **data_token, rt_uint32_t ch)
  741. {
  742. struct pbuf* p;
  743. p = pbuf_alloc(PBUF_LINK, buf_size, PBUF_RAM);
  744. if (RT_NULL == p) {
  745. rt_kprintf("DaVinci EMAC: failed to alloc pbuf\n");
  746. return RT_NULL;
  747. }
  748. /* set device pointer in p and reserve space for extra bytes */
  749. *data_token = (void *) p;
  750. EMAC_CACHE_WRITEBACK_INVALIDATE((unsigned long)p->payload, buf_size);
  751. return p->payload;
  752. }
  753. /**
  754. * emac_rx_bdproc: RX buffer descriptor (packet) processing
  755. * @priv: The DaVinci EMAC driver private structure
  756. * @ch: RX channel number to process buffer descriptors for
  757. * @budget: number of packets allowed to process
  758. *
  759. * Processes RX buffer descriptors - checks ownership bit on the RX buffer
  760. * descriptor, sends the receive packet to upper layer, allocates a new PBUF
  761. * and recycles the buffer descriptor (requeues it in hardware RX queue).
  762. * Only "budget" number of packets are processed and indication of pending
  763. * packets provided to the caller.
  764. *
  765. * Returns number of packets processed (and indication of pending packets)
  766. */
  767. static int emac_rx_bdproc(struct emac_priv *priv, rt_uint32_t ch, rt_uint32_t budget)
  768. {
  769. unsigned long flags;
  770. rt_uint32_t frame_status;
  771. rt_uint32_t pkts_processed = 0;
  772. char *new_buffer;
  773. struct emac_rx_bd __iomem *curr_bd;
  774. struct emac_rx_bd __iomem *last_bd;
  775. struct emac_netpktobj *curr_pkt, pkt_obj;
  776. struct emac_netbufobj buf_obj;
  777. struct emac_netbufobj *rx_buf_obj;
  778. void *new_buf_token;
  779. struct emac_rxch *rxch = priv->rxch[ch];
  780. if (1 == rxch->teardown_pending)
  781. return 0;
  782. ++rxch->proc_count;
  783. rt_sem_take(&priv->rx_lock, RT_WAITING_FOREVER);
  784. pkt_obj.buf_list = &buf_obj;
  785. curr_pkt = &pkt_obj;
  786. curr_bd = rxch->active_queue_head;
  787. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  788. frame_status = curr_bd->mode;
  789. while ((curr_bd) &&
  790. ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
  791. (pkts_processed < budget)) {
  792. new_buffer = emac_net_alloc_rx_buf(priv, rxch->buf_size,
  793. &new_buf_token, EMAC_DEF_RX_CH);
  794. if (RT_NULL == new_buffer) {
  795. ++rxch->out_of_rx_buffers;
  796. goto end_emac_rx_bdproc;
  797. }
  798. /* populate received packet data structure */
  799. rx_buf_obj = &curr_pkt->buf_list[0];
  800. rx_buf_obj->data_ptr = (char *)curr_bd->data_ptr;
  801. rx_buf_obj->length = curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE;
  802. rx_buf_obj->buf_token = curr_bd->buf_token;
  803. curr_pkt->pkt_token = curr_pkt->buf_list->buf_token;
  804. curr_pkt->num_bufs = 1;
  805. curr_pkt->pkt_length =
  806. (frame_status & EMAC_RX_BD_PKT_LENGTH_MASK);
  807. emac_write(EMAC_RXCP(ch), emac_virt_to_phys(curr_bd));
  808. ++rxch->processed_bd;
  809. last_bd = curr_bd;
  810. curr_bd = last_bd->next;
  811. rxch->active_queue_head = curr_bd;
  812. /* check if end of RX queue ? */
  813. if (frame_status & EMAC_CPPI_EOQ_BIT) {
  814. if (curr_bd) {
  815. ++rxch->mis_queued_packets;
  816. emac_write(EMAC_RXHDP(ch),
  817. emac_virt_to_phys(curr_bd));
  818. } else {
  819. ++rxch->end_of_queue;
  820. rxch->queue_active = 0;
  821. }
  822. }
  823. /* recycle BD */
  824. emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
  825. new_buf_token);
  826. /* return the packet to the user - BD ptr passed in
  827. * last parameter for potential *future* use */
  828. rt_sem_release(&priv->rx_lock);
  829. emac_net_rx_cb(priv, curr_pkt);//???
  830. rt_sem_take(&priv->rx_lock, RT_WAITING_FOREVER);
  831. curr_bd = rxch->active_queue_head;
  832. if (curr_bd) {
  833. BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
  834. frame_status = curr_bd->mode;
  835. }
  836. ++pkts_processed;
  837. }
  838. end_emac_rx_bdproc:
  839. rt_sem_release(&priv->rx_lock);
  840. return pkts_processed;
  841. }
  842. /* reception packet. */
  843. struct pbuf *rt_davinci_emac_rx(rt_device_t dev)
  844. {
  845. struct emac_priv *priv = dev->user_data;
  846. struct pbuf* p = RT_NULL;
  847. rt_uint32_t len;
  848. void *buffer;
  849. struct pbuf* q;
  850. rt_uint8_t *buf = RT_NULL;
  851. unsigned int mask;
  852. rt_uint32_t status = 0;
  853. rt_uint32_t num_pkts = 0;
  854. /* Check interrupt vectors and call packet processing */
  855. status = emac_read(EMAC_MACINVECTOR);
  856. mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
  857. if (priv->version == EMAC_VERSION_2)
  858. mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
  859. if (status & mask) {
  860. num_pkts = emac_tx_bdproc(priv, EMAC_DEF_TX_CH,
  861. EMAC_DEF_TX_MAX_SERVICE);
  862. } /* TX processing */
  863. /*if (num_pkts)
  864. //return budget;
  865. return RT_NULL;*/
  866. mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
  867. if (priv->version == EMAC_VERSION_2)
  868. mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
  869. if (status & mask) {
  870. num_pkts = emac_rx_bdproc(priv, EMAC_DEF_RX_CH, EMAC_DEF_RX_MAX_SERVICE);
  871. } /* RX processing */
  872. /*if (num_pkts < EMAC_DEF_RX_MAX_SERVICE) {
  873. emac_int_enable(priv);
  874. }*/
  875. mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
  876. if (priv->version == EMAC_VERSION_2)
  877. mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
  878. if (status & mask) {
  879. rt_uint32_t ch, cause;
  880. rt_kprintf("DaVinci EMAC: Fatal Hardware Error\n");
  881. status = emac_read(EMAC_MACSTATUS);
  882. cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
  883. EMAC_MACSTATUS_TXERRCODE_SHIFT);
  884. if (cause) {
  885. ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
  886. EMAC_MACSTATUS_TXERRCH_SHIFT);
  887. }
  888. cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
  889. EMAC_MACSTATUS_RXERRCODE_SHIFT);
  890. if (cause) {
  891. ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
  892. EMAC_MACSTATUS_RXERRCH_SHIFT);
  893. }
  894. } /* Host error processing */
  895. //return num_pkts;
  896. //return p;
  897. emac_int_enable(priv);
  898. return RT_NULL;
  899. }
  900. /**
  901. * emac_set_type0addr: Set EMAC Type0 mac address
  902. * @priv: The DaVinci EMAC driver private structure
  903. * @ch: RX channel number
  904. * @mac_addr: MAC address to set in device
  905. *
  906. * Called internally to set Type0 mac address of the Device
  907. *
  908. * Returns success (0) or appropriate error code (none as of now)
  909. */
  910. static void emac_set_type0addr(struct emac_priv *priv, rt_uint32_t ch, char *mac_addr)
  911. {
  912. rt_uint32_t val;
  913. val = ((mac_addr[5] << 8) | (mac_addr[4]));
  914. emac_write(EMAC_MACSRCADDRLO, val);
  915. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  916. (mac_addr[1] << 8) | (mac_addr[0]));
  917. emac_write(EMAC_MACSRCADDRHI, val);
  918. val = emac_read(EMAC_RXUNICASTSET);
  919. val |= BIT(ch);
  920. emac_write(EMAC_RXUNICASTSET, val);
  921. val = emac_read(EMAC_RXUNICASTCLEAR);
  922. val &= ~BIT(ch);
  923. emac_write(EMAC_RXUNICASTCLEAR, val);
  924. }
  925. /**
  926. * emac_set_type1addr: Set EMAC Type1 mac address
  927. * @priv: The DaVinci EMAC driver private structure
  928. * @ch: RX channel number
  929. * @mac_addr: MAC address to set in device
  930. *
  931. * Called internally to set Type1 mac address of the Device
  932. *
  933. * Returns success (0) or appropriate error code (none as of now)
  934. */
  935. static void emac_set_type1addr(struct emac_priv *priv, rt_uint32_t ch, char *mac_addr)
  936. {
  937. rt_uint32_t val;
  938. emac_write(EMAC_MACINDEX, ch);
  939. val = ((mac_addr[5] << 8) | mac_addr[4]);
  940. emac_write(EMAC_MACADDRLO, val);
  941. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  942. (mac_addr[1] << 8) | (mac_addr[0]));
  943. emac_write(EMAC_MACADDRHI, val);
  944. emac_set_type0addr(priv, ch, mac_addr);
  945. }
  946. /**
  947. * emac_set_type2addr: Set EMAC Type2 mac address
  948. * @priv: The DaVinci EMAC driver private structure
  949. * @ch: RX channel number
  950. * @mac_addr: MAC address to set in device
  951. * @index: index into RX address entries
  952. * @match: match parameter for RX address matching logic
  953. *
  954. * Called internally to set Type2 mac address of the Device
  955. *
  956. * Returns success (0) or appropriate error code (none as of now)
  957. */
  958. static void emac_set_type2addr(struct emac_priv *priv, rt_uint32_t ch,
  959. char *mac_addr, int index, int match)
  960. {
  961. rt_uint32_t val;
  962. emac_write(EMAC_MACINDEX, index);
  963. val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  964. (mac_addr[1] << 8) | (mac_addr[0]));
  965. emac_write(EMAC_MACADDRHI, val);
  966. val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
  967. (match << 19) | BIT(20));
  968. emac_write(EMAC_MACADDRLO, val);
  969. emac_set_type0addr(priv, ch, mac_addr);
  970. }
  971. /**
  972. * emac_setmac: Set mac address in the adapter (internal function)
  973. * @priv: The DaVinci EMAC driver private structure
  974. * @ch: RX channel number
  975. * @mac_addr: MAC address to set in device
  976. *
  977. * Called internally to set the mac address of the Device
  978. *
  979. * Returns success (0) or appropriate error code (none as of now)
  980. */
  981. static void emac_setmac(struct emac_priv *priv, rt_uint32_t ch, char *mac_addr)
  982. {
  983. if (priv->rx_addr_type == 0) {
  984. emac_set_type0addr(priv, ch, mac_addr);
  985. } else if (priv->rx_addr_type == 1) {
  986. rt_uint32_t cnt;
  987. for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
  988. emac_set_type1addr(priv, ch, mac_addr);
  989. } else if (priv->rx_addr_type == 2) {
  990. emac_set_type2addr(priv, ch, mac_addr, ch, 1);
  991. emac_set_type0addr(priv, ch, mac_addr);
  992. } else {
  993. rt_kprintf("DaVinci EMAC: Wrong addressing\n");
  994. }
  995. }
  996. /** EMAC on-chip buffer descriptor memory
  997. *
  998. * WARNING: Please note that the on chip memory is used for both TX and RX
  999. * buffer descriptor queues and is equally divided between TX and RX desc's
  1000. * If the number of TX or RX descriptors change this memory pointers need
  1001. * to be adjusted. If external memory is allocated then these pointers can
  1002. * pointer to the memory
  1003. *
  1004. */
  1005. #define EMAC_TX_BD_MEM(priv) ((priv)->emac_ctrl_ram)
  1006. #define EMAC_RX_BD_MEM(priv) ((priv)->emac_ctrl_ram + \
  1007. (((priv)->ctrl_ram_size) >> 1))
  1008. /**
  1009. * emac_init_txch: TX channel initialization
  1010. * @priv: The DaVinci EMAC driver private structure
  1011. * @ch: RX channel number
  1012. *
  1013. * Called during device init to setup a TX channel (allocate buffer desc
  1014. * create free pool and keep ready for transmission
  1015. *
  1016. * Returns success(0) or mem alloc failures error code
  1017. */
  1018. static int emac_init_txch(struct emac_priv *priv, rt_uint32_t ch)
  1019. {
  1020. rt_uint32_t cnt, bd_size;
  1021. void __iomem *mem;
  1022. struct emac_tx_bd __iomem *curr_bd;
  1023. struct emac_txch *txch = RT_NULL;
  1024. txch = rt_malloc(sizeof(struct emac_txch));
  1025. if (RT_NULL == txch) {
  1026. rt_kprintf("DaVinci EMAC: TX Ch mem alloc failed");
  1027. return -RT_ENOMEM;
  1028. }
  1029. rt_memset(txch, 0, sizeof(struct emac_txch));
  1030. priv->txch[ch] = txch;
  1031. txch->service_max = EMAC_DEF_TX_MAX_SERVICE;
  1032. txch->active_queue_head = RT_NULL;
  1033. txch->active_queue_tail = RT_NULL;
  1034. txch->queue_active = 0;
  1035. txch->teardown_pending = 0;
  1036. /* allocate memory for TX CPPI channel on a 4 byte boundry */
  1037. txch->tx_complete = rt_malloc(txch->service_max * sizeof(rt_uint32_t));
  1038. if (RT_NULL == txch->tx_complete) {
  1039. rt_kprintf("DaVinci EMAC: Tx service mem alloc failed");
  1040. rt_free(txch);
  1041. return -RT_ENOMEM;
  1042. }
  1043. memset(txch->tx_complete, 0, txch->service_max * sizeof(rt_uint32_t));
  1044. /* allocate buffer descriptor pool align every BD on four word
  1045. * boundry for future requirements */
  1046. bd_size = (sizeof(struct emac_tx_bd) + 0xF) & ~0xF;
  1047. txch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
  1048. txch->alloc_size = (((bd_size * txch->num_bd) + 0xF) & ~0xF);
  1049. /* alloc TX BD memory */
  1050. txch->bd_mem = EMAC_TX_BD_MEM(priv);
  1051. rt_memset((void *)txch->bd_mem, 0, txch->alloc_size);
  1052. /* initialize the BD linked list */
  1053. mem = (void __iomem *)
  1054. (((rt_uint32_t) txch->bd_mem + 0xF) & ~0xF);
  1055. txch->bd_pool_head = RT_NULL;
  1056. for (cnt = 0; cnt < txch->num_bd; cnt++) {
  1057. curr_bd = mem + (cnt * bd_size);
  1058. curr_bd->next = txch->bd_pool_head;
  1059. txch->bd_pool_head = curr_bd;
  1060. }
  1061. /* reset statistics counters */
  1062. txch->out_of_tx_bd = 0;
  1063. txch->no_active_pkts = 0;
  1064. txch->active_queue_count = 0;
  1065. return 0;
  1066. }
  1067. /**
  1068. * emac_init_rxch: RX channel initialization
  1069. * @priv: The DaVinci EMAC driver private structure
  1070. * @ch: RX channel number
  1071. * @param: mac address for RX channel
  1072. *
  1073. * Called during device init to setup a RX channel (allocate buffers and
  1074. * buffer descriptors, create queue and keep ready for reception
  1075. *
  1076. * Returns success(0) or mem alloc failures error code
  1077. */
  1078. static int emac_init_rxch(struct emac_priv *priv, rt_uint32_t ch, char *param)
  1079. {
  1080. rt_uint32_t cnt, bd_size;
  1081. void __iomem *mem;
  1082. struct emac_rx_bd __iomem *curr_bd;
  1083. struct emac_rxch *rxch = RT_NULL;
  1084. rxch = rt_malloc(sizeof(struct emac_rxch));
  1085. if (RT_NULL == rxch) {
  1086. rt_kprintf("DaVinci EMAC: RX Ch mem alloc failed");
  1087. return -ENOMEM;
  1088. }
  1089. rt_memset(rxch, 0, sizeof(struct emac_rxch));
  1090. priv->rxch[ch] = rxch;
  1091. rxch->buf_size = priv->rx_buf_size;
  1092. rxch->service_max = EMAC_DEF_RX_MAX_SERVICE;
  1093. rxch->queue_active = 0;
  1094. rxch->teardown_pending = 0;
  1095. /* save mac address */
  1096. for (cnt = 0; cnt < 6; cnt++)
  1097. rxch->mac_addr[cnt] = param[cnt];
  1098. /* allocate buffer descriptor pool align every BD on four word
  1099. * boundry for future requirements */
  1100. bd_size = (sizeof(struct emac_rx_bd) + 0xF) & ~0xF;
  1101. rxch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
  1102. rxch->alloc_size = (((bd_size * rxch->num_bd) + 0xF) & ~0xF);
  1103. rxch->bd_mem = EMAC_RX_BD_MEM(priv);
  1104. rt_memset((void *)rxch->bd_mem, 0, rxch->alloc_size);
  1105. rxch->pkt_queue.buf_list = &rxch->buf_queue;
  1106. /* allocate RX buffer and initialize the BD linked list */
  1107. mem = (void __iomem *)
  1108. (((rt_uint32_t) rxch->bd_mem + 0xF) & ~0xF);
  1109. rxch->active_queue_head = RT_NULL;
  1110. rxch->active_queue_tail = mem;
  1111. for (cnt = 0; cnt < rxch->num_bd; cnt++) {
  1112. curr_bd = mem + (cnt * bd_size);
  1113. /* for future use the last parameter contains the BD ptr */
  1114. curr_bd->data_ptr = emac_net_alloc_rx_buf(priv,
  1115. rxch->buf_size,
  1116. (void **)&curr_bd->buf_token,
  1117. EMAC_DEF_RX_CH);
  1118. if (curr_bd->data_ptr == RT_NULL) {
  1119. rt_kprintf("DaVinci EMAC: RX buf mem alloc " \
  1120. "failed for ch %d\n", ch);
  1121. rt_free(rxch);
  1122. return -RT_ENOMEM;
  1123. }
  1124. /* populate the hardware descriptor */
  1125. curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head);
  1126. curr_bd->buff_ptr = virt_to_phys(curr_bd->data_ptr);
  1127. curr_bd->off_b_len = rxch->buf_size;
  1128. curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
  1129. /* write back to hardware memory */
  1130. BD_CACHE_WRITEBACK_INVALIDATE((rt_uint32_t) curr_bd,
  1131. EMAC_BD_LENGTH_FOR_CACHE);
  1132. curr_bd->next = rxch->active_queue_head;
  1133. rxch->active_queue_head = curr_bd;
  1134. }
  1135. /* At this point rxCppi->activeQueueHead points to the first
  1136. RX BD ready to be given to RX HDP and rxch->active_queue_tail
  1137. points to the last RX BD
  1138. */
  1139. return 0;
  1140. }
  1141. /**
  1142. * emac_int_disable: Disable EMAC module interrupt
  1143. * @priv: The DaVinci EMAC driver private structure
  1144. *
  1145. * Disable EMAC interrupt
  1146. *
  1147. */
  1148. static void emac_int_disable(struct emac_priv *priv)
  1149. {
  1150. if (priv->version == EMAC_VERSION_2) {
  1151. unsigned long flags;
  1152. rt_interrupt_enter();
  1153. /* Program C0_Int_En to zero to turn off
  1154. * interrupts to the CPU */
  1155. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
  1156. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
  1157. /* NOTE: Rx Threshold and Misc interrupts are not disabled */
  1158. rt_interrupt_leave();
  1159. } else {
  1160. /* Set DM644x control registers for interrupt control */
  1161. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
  1162. }
  1163. }
  1164. /**
  1165. * emac_int_enable: Enable EMAC module interrupt
  1166. * @priv: The DaVinci EMAC driver private structure
  1167. *
  1168. * Enable EMAC interrupt
  1169. *
  1170. */
  1171. static void emac_int_enable(struct emac_priv *priv)
  1172. {
  1173. if (priv->version == EMAC_VERSION_2) {
  1174. /*if (priv->int_enable)
  1175. priv->int_enable();*/
  1176. emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
  1177. emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
  1178. /* In addition to turning on interrupt Enable, we need
  1179. * ack by writing appropriate values to the EOI
  1180. * register */
  1181. /* NOTE: Rx Threshold and Misc interrupts are not enabled */
  1182. /* ack rxen only then a new pulse will be generated */
  1183. emac_write(EMAC_DM646X_MACEOIVECTOR,
  1184. EMAC_DM646X_MAC_EOI_C0_RXEN);
  1185. /* ack txen- only then a new pulse will be generated */
  1186. emac_write(EMAC_DM646X_MACEOIVECTOR,
  1187. EMAC_DM646X_MAC_EOI_C0_TXEN);
  1188. } else {
  1189. /* Set DM644x control registers for interrupt control */
  1190. emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
  1191. }
  1192. }
  1193. /**
  1194. * emac_irq: EMAC interrupt handler
  1195. * @irq: interrupt number
  1196. * @param: EMAC isr parameters
  1197. *
  1198. */
  1199. static void emac_irq(int irq, void *param)
  1200. {
  1201. struct emac_priv *priv = param;
  1202. ++priv->isr_count;
  1203. emac_int_disable(priv);
  1204. eth_device_ready(&priv->parent);
  1205. }
  1206. /**
  1207. * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
  1208. * @priv: The DaVinci EMAC private adapter structure
  1209. *
  1210. * Enables EMAC hardware for packet processing - enables PHY, enables RX
  1211. * for packet reception and enables device interrupts
  1212. *
  1213. * Returns success (0) or appropriate error code (none right now)
  1214. */
  1215. static int emac_hw_enable(struct emac_priv *priv)
  1216. {
  1217. rt_uint32_t ch, val, mbp_enable, mac_control;
  1218. /* Soft reset */
  1219. emac_write(EMAC_SOFTRESET, 1);
  1220. while (emac_read(EMAC_SOFTRESET));
  1221. /* Disable interrupt & Set pacing for more interrupts initially */
  1222. emac_int_disable(priv);
  1223. /* Full duplex enable bit set when auto negotiation happens */
  1224. mac_control =
  1225. (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
  1226. ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
  1227. ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
  1228. ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
  1229. emac_write(EMAC_MACCONTROL, mac_control);
  1230. mbp_enable =
  1231. (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
  1232. ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
  1233. ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
  1234. ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
  1235. ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
  1236. ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
  1237. ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
  1238. ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
  1239. EMAC_RXMBP_PROMCH_SHIFT) |
  1240. ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
  1241. ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
  1242. EMAC_RXMBP_BROADCH_SHIFT) |
  1243. ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
  1244. ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
  1245. EMAC_RXMBP_MULTICH_SHIFT));
  1246. emac_write(EMAC_RXMBPENABLE, mbp_enable);
  1247. emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
  1248. EMAC_RX_MAX_LEN_MASK));
  1249. emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
  1250. EMAC_RX_BUFFER_OFFSET_MASK));
  1251. emac_write(EMAC_RXFILTERLOWTHRESH, 0);
  1252. emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
  1253. priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
  1254. val = emac_read(EMAC_TXCONTROL);
  1255. val |= EMAC_TX_CONTROL_TX_ENABLE_VAL;
  1256. emac_write(EMAC_TXCONTROL, val);
  1257. val = emac_read(EMAC_RXCONTROL);
  1258. val |= EMAC_RX_CONTROL_RX_ENABLE_VAL;
  1259. emac_write(EMAC_RXCONTROL, val);
  1260. emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
  1261. for (ch = 0; ch < EMAC_DEF_MAX_TX_CH; ch++) {
  1262. emac_write(EMAC_TXHDP(ch), 0);
  1263. emac_write(EMAC_TXINTMASKSET, BIT(ch));
  1264. }
  1265. for (ch = 0; ch < EMAC_DEF_MAX_RX_CH; ch++) {
  1266. struct emac_rxch *rxch = priv->rxch[ch];
  1267. emac_setmac(priv, ch, rxch->mac_addr);
  1268. emac_write(EMAC_RXINTMASKSET, BIT(ch));
  1269. rxch->queue_active = 1;
  1270. emac_write(EMAC_RXHDP(ch),
  1271. rxch->active_queue_head); /* physcal addr */
  1272. }
  1273. /* Enable MII */
  1274. val = emac_read(EMAC_MACCONTROL);
  1275. val |= (EMAC_MACCONTROL_GMIIEN);
  1276. emac_write(EMAC_MACCONTROL, val);
  1277. /* Enable interrupts */
  1278. emac_int_enable(priv);
  1279. return 0;
  1280. }
  1281. /**
  1282. * emac_dev_getnetstats: EMAC get statistics function
  1283. * @ndev: The DaVinci EMAC network adapter
  1284. *
  1285. * Called when system wants to get statistics from the device.
  1286. *
  1287. * We return the statistics in net_device_stats structure pulled from emac
  1288. */
  1289. static struct net_device_stats *emac_dev_getnetstats(struct emac_priv *priv)
  1290. {
  1291. rt_uint32_t mac_control;
  1292. rt_uint32_t stats_clear_mask;
  1293. /* update emac hardware stats and reset the registers*/
  1294. mac_control = emac_read(EMAC_MACCONTROL);
  1295. if (mac_control & EMAC_MACCONTROL_GMIIEN)
  1296. stats_clear_mask = EMAC_STATS_CLR_MASK;
  1297. else
  1298. stats_clear_mask = 0;
  1299. priv->net_dev_stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
  1300. emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
  1301. priv->net_dev_stats.collisions += (emac_read(EMAC_TXCOLLISION) +
  1302. emac_read(EMAC_TXSINGLECOLL) +
  1303. emac_read(EMAC_TXMULTICOLL));
  1304. emac_write(EMAC_TXCOLLISION, stats_clear_mask);
  1305. emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
  1306. emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
  1307. priv->net_dev_stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
  1308. emac_read(EMAC_RXJABBER) +
  1309. emac_read(EMAC_RXUNDERSIZED));
  1310. emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
  1311. emac_write(EMAC_RXJABBER, stats_clear_mask);
  1312. emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
  1313. priv->net_dev_stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
  1314. emac_read(EMAC_RXMOFOVERRUNS));
  1315. emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
  1316. emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
  1317. priv->net_dev_stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
  1318. emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
  1319. priv->net_dev_stats.tx_carrier_errors +=
  1320. emac_read(EMAC_TXCARRIERSENSE);
  1321. emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
  1322. priv->net_dev_stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
  1323. emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
  1324. return &priv->net_dev_stats;
  1325. }
  1326. /* RT-Thread Device Interface */
  1327. /* initialize the interface */
  1328. static rt_err_t rt_davinci_emac_init(rt_device_t dev)
  1329. {
  1330. struct emac_priv *priv = dev->user_data;
  1331. unsigned long paddr;
  1332. rt_uint32_t ch, rc;
  1333. int i;
  1334. /* Configuration items */
  1335. priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
  1336. /* Clear basic hardware */
  1337. for (ch = 0; ch < EMAC_MAX_TXRX_CHANNELS; ch++) {
  1338. emac_write(EMAC_TXHDP(ch), 0);
  1339. emac_write(EMAC_RXHDP(ch), 0);
  1340. emac_write(EMAC_RXHDP(ch), 0);
  1341. emac_write(EMAC_RXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
  1342. emac_write(EMAC_TXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
  1343. }
  1344. priv->mac_hash1 = 0;
  1345. priv->mac_hash2 = 0;
  1346. emac_write(EMAC_MACHASH1, 0);
  1347. emac_write(EMAC_MACHASH2, 0);
  1348. /* multi ch not supported - open 1 TX, 1RX ch by default */
  1349. rc = emac_init_txch(priv, EMAC_DEF_TX_CH);
  1350. if (0 != rc) {
  1351. rt_kprintf("DaVinci EMAC: emac_init_txch() failed");
  1352. return rc;
  1353. }
  1354. rc = emac_init_rxch(priv, EMAC_DEF_RX_CH, priv->mac_addr);
  1355. if (0 != rc) {
  1356. rt_kprintf("DaVinci EMAC: emac_init_rxch() failed");
  1357. return rc;
  1358. }
  1359. rt_hw_interrupt_install(IRQ_DM365_EMAC_RXPULSE, emac_irq,
  1360. (void *)priv, "EMAC_RXPULSE");
  1361. rt_hw_interrupt_umask(IRQ_DM365_EMAC_RXPULSE);
  1362. rt_hw_interrupt_install(IRQ_DM365_EMAC_TXPULSE, emac_irq,
  1363. (void *)priv, "EMAC_TXPULSE");
  1364. rt_hw_interrupt_umask(IRQ_DM365_EMAC_TXPULSE);
  1365. rt_hw_interrupt_install(IRQ_DM365_EMAC_RXTHRESH, emac_irq,
  1366. (void *)priv, "EMAC_RXTHRESH");
  1367. rt_hw_interrupt_umask(IRQ_DM365_EMAC_RXTHRESH);
  1368. rt_hw_interrupt_install(IRQ_DM365_EMAC_MISCPULSE, emac_irq,
  1369. (void *)priv, "EMAC_MISCPULSE");
  1370. rt_hw_interrupt_umask(IRQ_DM365_EMAC_MISCPULSE);
  1371. emac_mii_reset(priv);
  1372. davinci_emac_phy_init(dev);
  1373. /* Start/Enable EMAC hardware */
  1374. emac_hw_enable(priv);
  1375. rt_timer_init(&priv->timer, "link_timer",
  1376. davinci_emac_update_link,
  1377. (void *)priv,
  1378. RT_TICK_PER_SECOND,
  1379. RT_TIMER_FLAG_PERIODIC);
  1380. rt_timer_start(&priv->timer);
  1381. rt_kprintf("davinci emac initialized\n");
  1382. return RT_EOK;
  1383. }
  1384. static rt_err_t rt_davinci_emac_open(rt_device_t dev, rt_uint16_t oflag)
  1385. {
  1386. return RT_EOK;
  1387. }
  1388. static rt_err_t rt_davinci_emac_close(rt_device_t dev)
  1389. {
  1390. return RT_EOK;
  1391. }
  1392. static rt_size_t rt_davinci_emac_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  1393. {
  1394. rt_set_errno(-RT_ENOSYS);
  1395. return 0;
  1396. }
  1397. static rt_size_t rt_davinci_emac_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  1398. {
  1399. rt_set_errno(-RT_ENOSYS);
  1400. return 0;
  1401. }
  1402. static rt_err_t rt_davinci_emac_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  1403. {
  1404. struct emac_priv *priv = dev->user_data;
  1405. switch(cmd)
  1406. {
  1407. case NIOCTL_GADDR:
  1408. /* get mac address */
  1409. if(args) rt_memcpy(args, priv->mac_addr, 6);
  1410. else return -RT_ERROR;
  1411. break;
  1412. default :
  1413. break;
  1414. }
  1415. return RT_EOK;
  1416. }
  1417. void dm365_emac_gpio_init(void)
  1418. {
  1419. rt_uint32_t arm_intmux;
  1420. /*
  1421. * EMAC interrupts are multiplexed with GPIO interrupts
  1422. * Details are available at the DM365 ARM
  1423. * Subsystem Users Guide(sprufg5.pdf) pages 133 - 134
  1424. */
  1425. arm_intmux = davinci_readl(DM365_ARM_INTMUX);
  1426. arm_intmux |= (1 << 14)|(1 << 15)|(1 << 16)|(1 << 17);
  1427. davinci_writel(arm_intmux, DM365_ARM_INTMUX);
  1428. }
  1429. void rt_hw_davinci_emac_init()
  1430. {
  1431. struct emac_priv *priv = &davinci_emac_device;
  1432. struct clk *emac_clk;
  1433. emac_clk = clk_get("EMACCLK");
  1434. emac_bus_frequency = clk_get_rate(emac_clk);
  1435. psc_change_state(DAVINCI_DM365_LPSC_CPGMAC, PSC_ENABLE);
  1436. dm365_emac_gpio_init();
  1437. rt_memset(&davinci_emac_device, 0, sizeof(davinci_emac_device));
  1438. davinci_emac_device.emac_base = DM365_EMAC_CNTRL_BASE;
  1439. davinci_emac_device.ctrl_base = DM365_EMAC_WRAP_CNTRL_BASE;
  1440. davinci_emac_device.ctrl_ram_size = DM365_EMAC_CNTRL_RAM_SIZE;
  1441. davinci_emac_device.emac_ctrl_ram = DM365_EMAC_WRAP_RAM_BASE;
  1442. davinci_emac_device.mdio_base = DM365_EMAC_MDIO_BASE;
  1443. davinci_emac_device.version = EMAC_VERSION_2;
  1444. davinci_emac_device.rmii_en = 0;
  1445. davinci_emac_device.phy_addr = 0x09;
  1446. rt_sem_init(&sem_ack, "tx_ack", 0, RT_IPC_FLAG_FIFO);
  1447. rt_sem_init(&priv->tx_lock, "tx_lock", 1, RT_IPC_FLAG_FIFO);
  1448. rt_sem_init(&priv->rx_lock, "rx_lock", 1, RT_IPC_FLAG_FIFO);
  1449. davinci_emac_device.mac_addr[0] = 0x00;
  1450. davinci_emac_device.mac_addr[1] = 0x60;
  1451. davinci_emac_device.mac_addr[2] = 0x6E;
  1452. davinci_emac_device.mac_addr[3] = 0x11;
  1453. davinci_emac_device.mac_addr[4] = 0x22;
  1454. davinci_emac_device.mac_addr[5] = 0x33;
  1455. davinci_emac_device.parent.parent.init = rt_davinci_emac_init;
  1456. davinci_emac_device.parent.parent.open = rt_davinci_emac_open;
  1457. davinci_emac_device.parent.parent.close = rt_davinci_emac_close;
  1458. davinci_emac_device.parent.parent.read = rt_davinci_emac_read;
  1459. davinci_emac_device.parent.parent.write = rt_davinci_emac_write;
  1460. davinci_emac_device.parent.parent.control = rt_davinci_emac_control;
  1461. davinci_emac_device.parent.parent.user_data = &davinci_emac_device;
  1462. davinci_emac_device.parent.eth_rx = rt_davinci_emac_rx;
  1463. davinci_emac_device.parent.eth_tx = rt_davinci_emac_tx;
  1464. eth_device_init(&(davinci_emac_device.parent), "e0");
  1465. }
  1466. #ifdef RT_USING_FINSH
  1467. #include <finsh.h>
  1468. void dump_emac_stats(void)
  1469. {
  1470. int i;
  1471. struct emac_priv *emac;
  1472. struct net_device_stats *stats;
  1473. rt_device_t dev = rt_device_find("e0");
  1474. if(dev == RT_NULL)
  1475. return;
  1476. emac = (struct emac_priv *)dev->user_data;
  1477. stats = emac_dev_getnetstats(emac);
  1478. rt_kprintf("rx_packets = %d\n"
  1479. "tx_packets = %d\n"
  1480. "rx_bytes = %d\n"
  1481. "tx_bytes = %d\n"
  1482. "rx_errors = %d\n"
  1483. "tx_errors = %d\n"
  1484. "rx_dropped = %d\n"
  1485. "tx_dropped = %d\n"
  1486. "multicast = %d\n"
  1487. "collisions = %d\n",
  1488. stats->rx_packets,
  1489. stats->tx_packets,
  1490. stats->rx_bytes,
  1491. stats->tx_bytes,
  1492. stats->rx_errors,
  1493. stats->tx_errors,
  1494. stats->rx_dropped,
  1495. stats->tx_dropped,
  1496. stats->multicast,
  1497. stats->collisions);
  1498. rt_kprintf("rx_length_errors = %d\n"
  1499. "rx_over_errors = %d\n"
  1500. "rx_crc_errors = %d\n"
  1501. "rx_frame_errors = %d\n"
  1502. "rx_fifo_errors = %d\n"
  1503. "rx_missed_errors = %d\n",
  1504. stats->rx_length_errors,
  1505. stats->rx_over_errors,
  1506. stats->rx_crc_errors,
  1507. stats->rx_frame_errors,
  1508. stats->rx_fifo_errors,
  1509. stats->rx_missed_errors);
  1510. rt_kprintf("tx_aborted_errors = %d\n"
  1511. "tx_carrier_errors = %d\n"
  1512. "tx_fifo_errors = %d\n"
  1513. "tx_heartbeat_errors = %d\n"
  1514. "tx_window_errors = %d\n",
  1515. stats->tx_aborted_errors,
  1516. stats->tx_carrier_errors,
  1517. stats->tx_fifo_errors,
  1518. stats->tx_heartbeat_errors,
  1519. stats->tx_window_errors);
  1520. rt_kprintf("rx_compressed = %d\n"
  1521. "tx_compressed = %d\n",
  1522. stats->rx_compressed,
  1523. stats->tx_compressed);
  1524. rt_kprintf("\n");
  1525. }
  1526. FINSH_FUNCTION_EXPORT(dump_emac_stats, dump emac statistics);
  1527. #ifdef FINSH_USING_MSH
  1528. int cmd_dump_emac_stats(int argc, char** argv)
  1529. {
  1530. dump_emac_stats();
  1531. return 0;
  1532. }
  1533. FINSH_FUNCTION_EXPORT_ALIAS(cmd_dump_emac_stats, __cmd_dump_emac_stats, dump emac statistics.);
  1534. #endif
  1535. #endif