altera_tse.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Altera 10/100/1000 triple speed ethernet mac driver
  3. *
  4. * Copyright (C) 2008 Altera Corporation.
  5. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <malloc.h>
  14. #include <net.h>
  15. #include <command.h>
  16. #include <asm/cache.h>
  17. #include <asm/dma-mapping.h>
  18. #include <miiphy.h>
  19. #include "altera_tse.h"
  20. /* sgdma debug - print descriptor */
  21. static void alt_sgdma_print_desc(volatile struct alt_sgdma_descriptor *desc)
  22. {
  23. debug("SGDMA DEBUG :\n");
  24. debug("desc->source : 0x%x \n", (unsigned int)desc->source);
  25. debug("desc->destination : 0x%x \n", (unsigned int)desc->destination);
  26. debug("desc->next : 0x%x \n", (unsigned int)desc->next);
  27. debug("desc->source_pad : 0x%x \n", (unsigned int)desc->source_pad);
  28. debug("desc->destination_pad : 0x%x \n",
  29. (unsigned int)desc->destination_pad);
  30. debug("desc->next_pad : 0x%x \n", (unsigned int)desc->next_pad);
  31. debug("desc->bytes_to_transfer : 0x%x \n",
  32. (unsigned int)desc->bytes_to_transfer);
  33. debug("desc->actual_bytes_transferred : 0x%x \n",
  34. (unsigned int)desc->actual_bytes_transferred);
  35. debug("desc->descriptor_status : 0x%x \n",
  36. (unsigned int)desc->descriptor_status);
  37. debug("desc->descriptor_control : 0x%x \n",
  38. (unsigned int)desc->descriptor_control);
  39. }
  40. /* This is a generic routine that the SGDMA mode-specific routines
  41. * call to populate a descriptor.
  42. * arg1 :pointer to first SGDMA descriptor.
  43. * arg2 :pointer to next SGDMA descriptor.
  44. * arg3 :Address to where data to be written.
  45. * arg4 :Address from where data to be read.
  46. * arg5 :no of byte to transaction.
  47. * arg6 :variable indicating to generate start of packet or not
  48. * arg7 :read fixed
  49. * arg8 :write fixed
  50. * arg9 :read burst
  51. * arg10 :write burst
  52. * arg11 :atlantic_channel number
  53. */
  54. static void alt_sgdma_construct_descriptor_burst(
  55. volatile struct alt_sgdma_descriptor *desc,
  56. volatile struct alt_sgdma_descriptor *next,
  57. unsigned int *read_addr,
  58. unsigned int *write_addr,
  59. unsigned short length_or_eop,
  60. int generate_eop,
  61. int read_fixed,
  62. int write_fixed_or_sop,
  63. int read_burst,
  64. int write_burst,
  65. unsigned char atlantic_channel)
  66. {
  67. /*
  68. * Mark the "next" descriptor as "not" owned by hardware. This prevents
  69. * The SGDMA controller from continuing to process the chain. This is
  70. * done as a single IO write to bypass cache, without flushing
  71. * the entire descriptor, since only the 8-bit descriptor status must
  72. * be flushed.
  73. */
  74. if (!next)
  75. debug("Next descriptor not defined!!\n");
  76. next->descriptor_control = (next->descriptor_control &
  77. ~ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK);
  78. desc->source = (unsigned int *)((unsigned int)read_addr & 0x1FFFFFFF);
  79. desc->destination =
  80. (unsigned int *)((unsigned int)write_addr & 0x1FFFFFFF);
  81. desc->next = (unsigned int *)((unsigned int)next & 0x1FFFFFFF);
  82. desc->source_pad = 0x0;
  83. desc->destination_pad = 0x0;
  84. desc->next_pad = 0x0;
  85. desc->bytes_to_transfer = length_or_eop;
  86. desc->actual_bytes_transferred = 0;
  87. desc->descriptor_status = 0x0;
  88. /* SGDMA burst not currently supported */
  89. desc->read_burst = 0;
  90. desc->write_burst = 0;
  91. /*
  92. * Set the descriptor control block as follows:
  93. * - Set "owned by hardware" bit
  94. * - Optionally set "generate EOP" bit
  95. * - Optionally set the "read from fixed address" bit
  96. * - Optionally set the "write to fixed address bit (which serves
  97. * serves as a "generate SOP" control bit in memory-to-stream mode).
  98. * - Set the 4-bit atlantic channel, if specified
  99. *
  100. * Note this step is performed after all other descriptor information
  101. * has been filled out so that, if the controller already happens to be
  102. * pointing at this descriptor, it will not run (via the "owned by
  103. * hardware" bit) until all other descriptor has been set up.
  104. */
  105. desc->descriptor_control =
  106. ((ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK) |
  107. (generate_eop ?
  108. ALT_SGDMA_DESCRIPTOR_CONTROL_GENERATE_EOP_MSK : 0x0) |
  109. (read_fixed ?
  110. ALT_SGDMA_DESCRIPTOR_CONTROL_READ_FIXED_ADDRESS_MSK : 0x0) |
  111. (write_fixed_or_sop ?
  112. ALT_SGDMA_DESCRIPTOR_CONTROL_WRITE_FIXED_ADDRESS_MSK : 0x0) |
  113. (atlantic_channel ? ((atlantic_channel & 0x0F) << 3) : 0)
  114. );
  115. }
  116. static int alt_sgdma_do_sync_transfer(volatile struct alt_sgdma_registers *dev,
  117. volatile struct alt_sgdma_descriptor *desc)
  118. {
  119. unsigned int status;
  120. int counter = 0;
  121. /* Wait for any pending transfers to complete */
  122. alt_sgdma_print_desc(desc);
  123. status = dev->status;
  124. counter = 0;
  125. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  126. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  127. break;
  128. }
  129. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  130. debug("Timeout waiting sgdma in do sync!\n");
  131. /*
  132. * Clear any (previous) status register information
  133. * that might occlude our error checking later.
  134. */
  135. dev->status = 0xFF;
  136. /* Point the controller at the descriptor */
  137. dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
  138. debug("next desc in sgdma 0x%x\n",
  139. (unsigned int)dev->next_descriptor_pointer);
  140. /*
  141. * Set up SGDMA controller to:
  142. * - Disable interrupt generation
  143. * - Run once a valid descriptor is written to controller
  144. * - Stop on an error with any particular descriptor
  145. */
  146. dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
  147. ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
  148. /* Wait for the descriptor (chain) to complete */
  149. status = dev->status;
  150. debug("wait for sgdma....");
  151. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK)
  152. ;
  153. debug("done\n");
  154. /* Clear Run */
  155. dev->control = (dev->control & (~ALT_SGDMA_CONTROL_RUN_MSK));
  156. /* Get & clear status register contents */
  157. status = dev->status;
  158. dev->status = 0xFF;
  159. /* we really should check if the transfer completes properly */
  160. debug("tx sgdma status = 0x%x", status);
  161. return 0;
  162. }
  163. static int alt_sgdma_do_async_transfer(volatile struct alt_sgdma_registers *dev,
  164. volatile struct alt_sgdma_descriptor *desc)
  165. {
  166. unsigned int status;
  167. int counter = 0;
  168. /* Wait for any pending transfers to complete */
  169. alt_sgdma_print_desc(desc);
  170. status = dev->status;
  171. counter = 0;
  172. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  173. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  174. break;
  175. }
  176. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  177. debug("Timeout waiting sgdma in do async!\n");
  178. /*
  179. * Clear the RUN bit in the control register. This is needed
  180. * to restart the SGDMA engine later on.
  181. */
  182. dev->control = 0;
  183. /*
  184. * Clear any (previous) status register information
  185. * that might occlude our error checking later.
  186. */
  187. dev->status = 0xFF;
  188. /* Point the controller at the descriptor */
  189. dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
  190. /*
  191. * Set up SGDMA controller to:
  192. * - Disable interrupt generation
  193. * - Run once a valid descriptor is written to controller
  194. * - Stop on an error with any particular descriptor
  195. */
  196. dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
  197. ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
  198. /* we really should check if the transfer completes properly */
  199. return 0;
  200. }
  201. /* u-boot interface */
  202. static int tse_adjust_link(struct altera_tse_priv *priv)
  203. {
  204. unsigned int refvar;
  205. refvar = priv->mac_dev->command_config.image;
  206. if (!(priv->duplexity))
  207. refvar |= ALTERA_TSE_CMD_HD_ENA_MSK;
  208. else
  209. refvar &= ~ALTERA_TSE_CMD_HD_ENA_MSK;
  210. switch (priv->speed) {
  211. case 1000:
  212. refvar |= ALTERA_TSE_CMD_ETH_SPEED_MSK;
  213. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  214. break;
  215. case 100:
  216. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  217. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  218. break;
  219. case 10:
  220. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  221. refvar |= ALTERA_TSE_CMD_ENA_10_MSK;
  222. break;
  223. }
  224. priv->mac_dev->command_config.image = refvar;
  225. return 0;
  226. }
  227. static int tse_eth_send(struct eth_device *dev, void *packet, int length)
  228. {
  229. struct altera_tse_priv *priv = dev->priv;
  230. volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  231. volatile struct alt_sgdma_descriptor *tx_desc =
  232. (volatile struct alt_sgdma_descriptor *)priv->tx_desc;
  233. volatile struct alt_sgdma_descriptor *tx_desc_cur =
  234. (volatile struct alt_sgdma_descriptor *)&tx_desc[0];
  235. flush_dcache_range((unsigned long)packet,
  236. (unsigned long)packet + length);
  237. alt_sgdma_construct_descriptor_burst(
  238. (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
  239. (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
  240. (unsigned int *)packet, /* read addr */
  241. (unsigned int *)0,
  242. length, /* length or EOP ,will change for each tx */
  243. 0x1, /* gen eop */
  244. 0x0, /* read fixed */
  245. 0x1, /* write fixed or sop */
  246. 0x0, /* read burst */
  247. 0x0, /* write burst */
  248. 0x0 /* channel */
  249. );
  250. debug("TX Packet @ 0x%x,0x%x bytes", (unsigned int)packet, length);
  251. /* send the packet */
  252. debug("sending packet\n");
  253. alt_sgdma_do_sync_transfer(tx_sgdma, tx_desc_cur);
  254. debug("sent %d bytes\n", tx_desc_cur->actual_bytes_transferred);
  255. return tx_desc_cur->actual_bytes_transferred;
  256. }
  257. static int tse_eth_rx(struct eth_device *dev)
  258. {
  259. int packet_length = 0;
  260. struct altera_tse_priv *priv = dev->priv;
  261. volatile struct alt_sgdma_descriptor *rx_desc =
  262. (volatile struct alt_sgdma_descriptor *)priv->rx_desc;
  263. volatile struct alt_sgdma_descriptor *rx_desc_cur = &rx_desc[0];
  264. if (rx_desc_cur->descriptor_status &
  265. ALT_SGDMA_DESCRIPTOR_STATUS_TERMINATED_BY_EOP_MSK) {
  266. debug("got packet\n");
  267. packet_length = rx_desc->actual_bytes_transferred;
  268. NetReceive(NetRxPackets[0], packet_length);
  269. /* start descriptor again */
  270. flush_dcache_range((unsigned long)(NetRxPackets[0]),
  271. (unsigned long)(NetRxPackets[0]) + PKTSIZE_ALIGN);
  272. alt_sgdma_construct_descriptor_burst(
  273. (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
  274. (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
  275. (unsigned int)0x0, /* read addr */
  276. (unsigned int *)NetRxPackets[0],
  277. 0x0, /* length or EOP */
  278. 0x0, /* gen eop */
  279. 0x0, /* read fixed */
  280. 0x0, /* write fixed or sop */
  281. 0x0, /* read burst */
  282. 0x0, /* write burst */
  283. 0x0 /* channel */
  284. );
  285. /* setup the sgdma */
  286. alt_sgdma_do_async_transfer(priv->sgdma_rx, &rx_desc[0]);
  287. return packet_length;
  288. }
  289. return -1;
  290. }
  291. static void tse_eth_halt(struct eth_device *dev)
  292. {
  293. /* don't do anything! */
  294. /* this gets called after each uboot */
  295. /* network command. don't need to reset the thing all of the time */
  296. }
  297. static void tse_eth_reset(struct eth_device *dev)
  298. {
  299. /* stop sgdmas, disable tse receive */
  300. struct altera_tse_priv *priv = dev->priv;
  301. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  302. volatile struct alt_sgdma_registers *rx_sgdma = priv->sgdma_rx;
  303. volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  304. int counter;
  305. volatile struct alt_sgdma_descriptor *rx_desc =
  306. (volatile struct alt_sgdma_descriptor *)&priv->rx_desc[0];
  307. /* clear rx desc & wait for sgdma to complete */
  308. rx_desc->descriptor_control = 0;
  309. rx_sgdma->control = 0;
  310. counter = 0;
  311. while (rx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  312. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  313. break;
  314. }
  315. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
  316. debug("Timeout waiting for rx sgdma!\n");
  317. rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  318. rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  319. }
  320. counter = 0;
  321. tx_sgdma->control = 0;
  322. while (tx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  323. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  324. break;
  325. }
  326. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
  327. debug("Timeout waiting for tx sgdma!\n");
  328. tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  329. tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  330. }
  331. /* reset the mac */
  332. mac_dev->command_config.bits.transmit_enable = 1;
  333. mac_dev->command_config.bits.receive_enable = 1;
  334. mac_dev->command_config.bits.software_reset = 1;
  335. counter = 0;
  336. while (mac_dev->command_config.bits.software_reset) {
  337. if (counter++ > ALT_TSE_SW_RESET_WATCHDOG_CNTR)
  338. break;
  339. }
  340. if (counter >= ALT_TSE_SW_RESET_WATCHDOG_CNTR)
  341. debug("TSEMAC SW reset bit never cleared!\n");
  342. }
  343. static int tse_mdio_read(struct altera_tse_priv *priv, unsigned int regnum)
  344. {
  345. volatile struct alt_tse_mac *mac_dev;
  346. unsigned int *mdio_regs;
  347. unsigned int data;
  348. u16 value;
  349. mac_dev = priv->mac_dev;
  350. /* set mdio address */
  351. mac_dev->mdio_phy1_addr = priv->phyaddr;
  352. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  353. /* get the data */
  354. data = mdio_regs[regnum];
  355. value = data & 0xffff;
  356. return value;
  357. }
  358. static int tse_mdio_write(struct altera_tse_priv *priv, unsigned int regnum,
  359. unsigned int value)
  360. {
  361. volatile struct alt_tse_mac *mac_dev;
  362. unsigned int *mdio_regs;
  363. unsigned int data;
  364. mac_dev = priv->mac_dev;
  365. /* set mdio address */
  366. mac_dev->mdio_phy1_addr = priv->phyaddr;
  367. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  368. /* get the data */
  369. data = (unsigned int)value;
  370. mdio_regs[regnum] = data;
  371. return 0;
  372. }
  373. /* MDIO access to phy */
  374. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  375. static int altera_tse_miiphy_write(const char *devname, unsigned char addr,
  376. unsigned char reg, unsigned short value)
  377. {
  378. struct eth_device *dev;
  379. struct altera_tse_priv *priv;
  380. dev = eth_get_dev_by_name(devname);
  381. priv = dev->priv;
  382. tse_mdio_write(priv, (uint) reg, (uint) value);
  383. return 0;
  384. }
  385. static int altera_tse_miiphy_read(const char *devname, unsigned char addr,
  386. unsigned char reg, unsigned short *value)
  387. {
  388. struct eth_device *dev;
  389. struct altera_tse_priv *priv;
  390. volatile struct alt_tse_mac *mac_dev;
  391. unsigned int *mdio_regs;
  392. dev = eth_get_dev_by_name(devname);
  393. priv = dev->priv;
  394. mac_dev = priv->mac_dev;
  395. mac_dev->mdio_phy1_addr = (int)addr;
  396. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  397. *value = 0xffff & mdio_regs[reg];
  398. return 0;
  399. }
  400. #endif
  401. /*
  402. * Also copied from tsec.c
  403. */
  404. /* Parse the status register for link, and then do
  405. * auto-negotiation
  406. */
  407. static uint mii_parse_sr(uint mii_reg, struct altera_tse_priv *priv)
  408. {
  409. /*
  410. * Wait if the link is up, and autonegotiation is in progress
  411. * (ie - we're capable and it's not done)
  412. */
  413. mii_reg = tse_mdio_read(priv, MIIM_STATUS);
  414. if (!(mii_reg & MIIM_STATUS_LINK) && (mii_reg & BMSR_ANEGCAPABLE)
  415. && !(mii_reg & BMSR_ANEGCOMPLETE)) {
  416. int i = 0;
  417. puts("Waiting for PHY auto negotiation to complete");
  418. while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
  419. /*
  420. * Timeout reached ?
  421. */
  422. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  423. puts(" TIMEOUT !\n");
  424. priv->link = 0;
  425. return 0;
  426. }
  427. if ((i++ % 1000) == 0)
  428. putc('.');
  429. udelay(1000); /* 1 ms */
  430. mii_reg = tse_mdio_read(priv, MIIM_STATUS);
  431. }
  432. puts(" done\n");
  433. priv->link = 1;
  434. udelay(500000); /* another 500 ms (results in faster booting) */
  435. } else {
  436. if (mii_reg & MIIM_STATUS_LINK) {
  437. debug("Link is up\n");
  438. priv->link = 1;
  439. } else {
  440. debug("Link is down\n");
  441. priv->link = 0;
  442. }
  443. }
  444. return 0;
  445. }
  446. /* Parse the 88E1011's status register for speed and duplex
  447. * information
  448. */
  449. static uint mii_parse_88E1011_psr(uint mii_reg, struct altera_tse_priv *priv)
  450. {
  451. uint speed;
  452. mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
  453. if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
  454. !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  455. int i = 0;
  456. puts("Waiting for PHY realtime link");
  457. while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  458. /* Timeout reached ? */
  459. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  460. puts(" TIMEOUT !\n");
  461. priv->link = 0;
  462. break;
  463. }
  464. if ((i++ == 1000) == 0) {
  465. i = 0;
  466. puts(".");
  467. }
  468. udelay(1000); /* 1 ms */
  469. mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
  470. }
  471. puts(" done\n");
  472. udelay(500000); /* another 500 ms (results in faster booting) */
  473. } else {
  474. if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
  475. priv->link = 1;
  476. else
  477. priv->link = 0;
  478. }
  479. if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
  480. priv->duplexity = 1;
  481. else
  482. priv->duplexity = 0;
  483. speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
  484. switch (speed) {
  485. case MIIM_88E1011_PHYSTAT_GBIT:
  486. priv->speed = 1000;
  487. debug("PHY Speed is 1000Mbit\n");
  488. break;
  489. case MIIM_88E1011_PHYSTAT_100:
  490. debug("PHY Speed is 100Mbit\n");
  491. priv->speed = 100;
  492. break;
  493. default:
  494. debug("PHY Speed is 10Mbit\n");
  495. priv->speed = 10;
  496. }
  497. return 0;
  498. }
  499. static uint mii_m88e1111s_setmode_sr(uint mii_reg, struct altera_tse_priv *priv)
  500. {
  501. uint mii_data = tse_mdio_read(priv, mii_reg);
  502. mii_data &= 0xfff0;
  503. if ((priv->flags >= 1) && (priv->flags <= 4))
  504. mii_data |= 0xb;
  505. else if (priv->flags == 5)
  506. mii_data |= 0x4;
  507. return mii_data;
  508. }
  509. static uint mii_m88e1111s_setmode_cr(uint mii_reg, struct altera_tse_priv *priv)
  510. {
  511. uint mii_data = tse_mdio_read(priv, mii_reg);
  512. mii_data &= ~0x82;
  513. if ((priv->flags >= 1) && (priv->flags <= 4))
  514. mii_data |= 0x82;
  515. return mii_data;
  516. }
  517. /*
  518. * Returns which value to write to the control register.
  519. * For 10/100, the value is slightly different
  520. */
  521. static uint mii_cr_init(uint mii_reg, struct altera_tse_priv *priv)
  522. {
  523. return MIIM_CONTROL_INIT;
  524. }
  525. /*
  526. * PHY & MDIO code
  527. * Need to add SGMII stuff
  528. *
  529. */
  530. static struct phy_info phy_info_M88E1111S = {
  531. 0x01410cc,
  532. "Marvell 88E1111S",
  533. 4,
  534. (struct phy_cmd[]){ /* config */
  535. /* Reset and configure the PHY */
  536. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  537. {MIIM_88E1111_PHY_EXT_SR, 0x848f,
  538. &mii_m88e1111s_setmode_sr},
  539. /* Delay RGMII TX and RX */
  540. {MIIM_88E1111_PHY_EXT_CR, 0x0cd2,
  541. &mii_m88e1111s_setmode_cr},
  542. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  543. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  544. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  545. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  546. {miim_end,}
  547. },
  548. (struct phy_cmd[]){ /* startup */
  549. /* Status is read once to clear old link state */
  550. {MIIM_STATUS, miim_read, NULL},
  551. /* Auto-negotiate */
  552. {MIIM_STATUS, miim_read, &mii_parse_sr},
  553. /* Read the status */
  554. {MIIM_88E1011_PHY_STATUS, miim_read,
  555. &mii_parse_88E1011_psr},
  556. {miim_end,}
  557. },
  558. (struct phy_cmd[]){ /* shutdown */
  559. {miim_end,}
  560. },
  561. };
  562. /* a generic flavor. */
  563. static struct phy_info phy_info_generic = {
  564. 0,
  565. "Unknown/Generic PHY",
  566. 32,
  567. (struct phy_cmd[]){ /* config */
  568. {MII_BMCR, BMCR_RESET, NULL},
  569. {MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART, NULL},
  570. {miim_end,}
  571. },
  572. (struct phy_cmd[]){ /* startup */
  573. {MII_BMSR, miim_read, NULL},
  574. {MII_BMSR, miim_read, &mii_parse_sr},
  575. {miim_end,}
  576. },
  577. (struct phy_cmd[]){ /* shutdown */
  578. {miim_end,}
  579. }
  580. };
  581. static struct phy_info *phy_info[] = {
  582. &phy_info_M88E1111S,
  583. NULL
  584. };
  585. /* Grab the identifier of the device's PHY, and search through
  586. * all of the known PHYs to see if one matches. If so, return
  587. * it, if not, return NULL
  588. */
  589. static struct phy_info *get_phy_info(struct eth_device *dev)
  590. {
  591. struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
  592. uint phy_reg, phy_ID;
  593. int i;
  594. struct phy_info *theInfo = NULL;
  595. /* Grab the bits from PHYIR1, and put them in the upper half */
  596. phy_reg = tse_mdio_read(priv, MIIM_PHYIR1);
  597. phy_ID = (phy_reg & 0xffff) << 16;
  598. /* Grab the bits from PHYIR2, and put them in the lower half */
  599. phy_reg = tse_mdio_read(priv, MIIM_PHYIR2);
  600. phy_ID |= (phy_reg & 0xffff);
  601. /* loop through all the known PHY types, and find one that */
  602. /* matches the ID we read from the PHY. */
  603. for (i = 0; phy_info[i]; i++) {
  604. if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
  605. theInfo = phy_info[i];
  606. break;
  607. }
  608. }
  609. if (theInfo == NULL) {
  610. theInfo = &phy_info_generic;
  611. debug("%s: No support for PHY id %x; assuming generic\n",
  612. dev->name, phy_ID);
  613. } else
  614. debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
  615. return theInfo;
  616. }
  617. /* Execute the given series of commands on the given device's
  618. * PHY, running functions as necessary
  619. */
  620. static void phy_run_commands(struct altera_tse_priv *priv, struct phy_cmd *cmd)
  621. {
  622. int i;
  623. uint result;
  624. for (i = 0; cmd->mii_reg != miim_end; i++) {
  625. if (cmd->mii_data == miim_read) {
  626. result = tse_mdio_read(priv, cmd->mii_reg);
  627. if (cmd->funct != NULL)
  628. (*(cmd->funct)) (result, priv);
  629. } else {
  630. if (cmd->funct != NULL)
  631. result = (*(cmd->funct)) (cmd->mii_reg, priv);
  632. else
  633. result = cmd->mii_data;
  634. tse_mdio_write(priv, cmd->mii_reg, result);
  635. }
  636. cmd++;
  637. }
  638. }
  639. /* Phy init code */
  640. static int init_phy(struct eth_device *dev)
  641. {
  642. struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
  643. struct phy_info *curphy;
  644. /* Get the cmd structure corresponding to the attached
  645. * PHY */
  646. curphy = get_phy_info(dev);
  647. if (curphy == NULL) {
  648. priv->phyinfo = NULL;
  649. debug("%s: No PHY found\n", dev->name);
  650. return 0;
  651. } else
  652. debug("%s found\n", curphy->name);
  653. priv->phyinfo = curphy;
  654. phy_run_commands(priv, priv->phyinfo->config);
  655. return 1;
  656. }
  657. static int tse_set_mac_address(struct eth_device *dev)
  658. {
  659. struct altera_tse_priv *priv = dev->priv;
  660. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  661. debug("Setting MAC address to 0x%02x%02x%02x%02x%02x%02x\n",
  662. dev->enetaddr[5], dev->enetaddr[4],
  663. dev->enetaddr[3], dev->enetaddr[2],
  664. dev->enetaddr[1], dev->enetaddr[0]);
  665. mac_dev->mac_addr_0 = ((dev->enetaddr[3]) << 24 |
  666. (dev->enetaddr[2]) << 16 |
  667. (dev->enetaddr[1]) << 8 | (dev->enetaddr[0]));
  668. mac_dev->mac_addr_1 = ((dev->enetaddr[5] << 8 |
  669. (dev->enetaddr[4])) & 0xFFFF);
  670. /* Set the MAC address */
  671. mac_dev->supp_mac_addr_0_0 = mac_dev->mac_addr_0;
  672. mac_dev->supp_mac_addr_0_1 = mac_dev->mac_addr_1;
  673. /* Set the MAC address */
  674. mac_dev->supp_mac_addr_1_0 = mac_dev->mac_addr_0;
  675. mac_dev->supp_mac_addr_1_1 = mac_dev->mac_addr_1;
  676. /* Set the MAC address */
  677. mac_dev->supp_mac_addr_2_0 = mac_dev->mac_addr_0;
  678. mac_dev->supp_mac_addr_2_1 = mac_dev->mac_addr_1;
  679. /* Set the MAC address */
  680. mac_dev->supp_mac_addr_3_0 = mac_dev->mac_addr_0;
  681. mac_dev->supp_mac_addr_3_1 = mac_dev->mac_addr_1;
  682. return 0;
  683. }
  684. static int tse_eth_init(struct eth_device *dev, bd_t * bd)
  685. {
  686. int dat;
  687. struct altera_tse_priv *priv = dev->priv;
  688. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  689. volatile struct alt_sgdma_descriptor *tx_desc = priv->tx_desc;
  690. volatile struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  691. volatile struct alt_sgdma_descriptor *rx_desc_cur =
  692. (volatile struct alt_sgdma_descriptor *)&rx_desc[0];
  693. /* stop controller */
  694. debug("Reseting TSE & SGDMAs\n");
  695. tse_eth_reset(dev);
  696. /* start the phy */
  697. debug("Configuring PHY\n");
  698. phy_run_commands(priv, priv->phyinfo->startup);
  699. /* need to create sgdma */
  700. debug("Configuring tx desc\n");
  701. alt_sgdma_construct_descriptor_burst(
  702. (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
  703. (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
  704. (unsigned int *)NULL, /* read addr */
  705. (unsigned int *)0,
  706. 0, /* length or EOP ,will change for each tx */
  707. 0x1, /* gen eop */
  708. 0x0, /* read fixed */
  709. 0x1, /* write fixed or sop */
  710. 0x0, /* read burst */
  711. 0x0, /* write burst */
  712. 0x0 /* channel */
  713. );
  714. debug("Configuring rx desc\n");
  715. flush_dcache_range((unsigned long)(NetRxPackets[0]),
  716. (unsigned long)(NetRxPackets[0]) + PKTSIZE_ALIGN);
  717. alt_sgdma_construct_descriptor_burst(
  718. (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
  719. (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
  720. (unsigned int)0x0, /* read addr */
  721. (unsigned int *)NetRxPackets[0],
  722. 0x0, /* length or EOP */
  723. 0x0, /* gen eop */
  724. 0x0, /* read fixed */
  725. 0x0, /* write fixed or sop */
  726. 0x0, /* read burst */
  727. 0x0, /* write burst */
  728. 0x0 /* channel */
  729. );
  730. /* start rx async transfer */
  731. debug("Starting rx sgdma\n");
  732. alt_sgdma_do_async_transfer(priv->sgdma_rx, rx_desc_cur);
  733. /* start TSE */
  734. debug("Configuring TSE Mac\n");
  735. /* Initialize MAC registers */
  736. mac_dev->max_frame_length = PKTSIZE_ALIGN;
  737. mac_dev->rx_almost_empty_threshold = 8;
  738. mac_dev->rx_almost_full_threshold = 8;
  739. mac_dev->tx_almost_empty_threshold = 8;
  740. mac_dev->tx_almost_full_threshold = 3;
  741. mac_dev->tx_sel_empty_threshold =
  742. CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
  743. mac_dev->tx_sel_full_threshold = 0;
  744. mac_dev->rx_sel_empty_threshold =
  745. CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
  746. mac_dev->rx_sel_full_threshold = 0;
  747. /* NO Shift */
  748. mac_dev->rx_cmd_stat.bits.rx_shift16 = 0;
  749. mac_dev->tx_cmd_stat.bits.tx_shift16 = 0;
  750. /* enable MAC */
  751. dat = 0;
  752. dat = ALTERA_TSE_CMD_TX_ENA_MSK | ALTERA_TSE_CMD_RX_ENA_MSK;
  753. mac_dev->command_config.image = dat;
  754. /* configure the TSE core */
  755. /* -- output clocks, */
  756. /* -- and later config stuff for SGMII */
  757. if (priv->link) {
  758. debug("Adjusting TSE to link speed\n");
  759. tse_adjust_link(priv);
  760. }
  761. return priv->link ? 0 : -1;
  762. }
  763. /* TSE init code */
  764. int altera_tse_initialize(u8 dev_num, int mac_base,
  765. int sgdma_rx_base, int sgdma_tx_base,
  766. u32 sgdma_desc_base, u32 sgdma_desc_size)
  767. {
  768. struct altera_tse_priv *priv;
  769. struct eth_device *dev;
  770. struct alt_sgdma_descriptor *rx_desc;
  771. struct alt_sgdma_descriptor *tx_desc;
  772. unsigned long dma_handle;
  773. dev = (struct eth_device *)malloc(sizeof *dev);
  774. if (NULL == dev)
  775. return 0;
  776. memset(dev, 0, sizeof *dev);
  777. priv = malloc(sizeof(*priv));
  778. if (!priv) {
  779. free(dev);
  780. return 0;
  781. }
  782. if (sgdma_desc_size) {
  783. if (sgdma_desc_size < (sizeof(*tx_desc) * (3 + PKTBUFSRX))) {
  784. printf("ALTERA_TSE-%hu: "
  785. "descriptor memory is too small\n", dev_num);
  786. free(priv);
  787. free(dev);
  788. return 0;
  789. }
  790. tx_desc = (struct alt_sgdma_descriptor *)sgdma_desc_base;
  791. } else {
  792. tx_desc = dma_alloc_coherent(sizeof(*tx_desc) * (3 + PKTBUFSRX),
  793. &dma_handle);
  794. }
  795. rx_desc = tx_desc + 2;
  796. debug("tx desc: address = 0x%x\n", (unsigned int)tx_desc);
  797. debug("rx desc: address = 0x%x\n", (unsigned int)rx_desc);
  798. if (!tx_desc) {
  799. free(priv);
  800. free(dev);
  801. return 0;
  802. }
  803. memset(rx_desc, 0, (sizeof *rx_desc) * (PKTBUFSRX + 1));
  804. memset(tx_desc, 0, (sizeof *tx_desc) * 2);
  805. /* initialize tse priv */
  806. priv->mac_dev = (volatile struct alt_tse_mac *)mac_base;
  807. priv->sgdma_rx = (volatile struct alt_sgdma_registers *)sgdma_rx_base;
  808. priv->sgdma_tx = (volatile struct alt_sgdma_registers *)sgdma_tx_base;
  809. priv->phyaddr = CONFIG_SYS_ALTERA_TSE_PHY_ADDR;
  810. priv->flags = CONFIG_SYS_ALTERA_TSE_FLAGS;
  811. priv->rx_desc = rx_desc;
  812. priv->tx_desc = tx_desc;
  813. /* init eth structure */
  814. dev->priv = priv;
  815. dev->init = tse_eth_init;
  816. dev->halt = tse_eth_halt;
  817. dev->send = tse_eth_send;
  818. dev->recv = tse_eth_rx;
  819. dev->write_hwaddr = tse_set_mac_address;
  820. sprintf(dev->name, "%s-%hu", "ALTERA_TSE", dev_num);
  821. eth_register(dev);
  822. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  823. miiphy_register(dev->name, altera_tse_miiphy_read,
  824. altera_tse_miiphy_write);
  825. #endif
  826. init_phy(dev);
  827. return 1;
  828. }